diff --git a/116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.py b/116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.py index 11ab077..60d401f 100644 --- a/116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.py +++ b/116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.py @@ -28,16 +28,13 @@ def __init__(self, val, left, right, next): self.next = next """ class Solution: - def connect(self, root: 'Node') -> 'Node': - if not root: - return None - if root.left: - root.left.next = root.right + def connect(self, root: 'Optional[Node]') -> 'Optional[Node]': + if not root: return None if root.right: + if root.left: + root.left.next = root.right if root.next: root.right.next = root.next.left - else: - root.right.next = None self.connect(root.left) self.connect(root.right) return root diff --git a/1325. Delete Leaves With a Given Value/1325. Delete Leaves With a Given Value.py b/1325. Delete Leaves With a Given Value/1325. Delete Leaves With a Given Value.py new file mode 100644 index 0000000..4cc6325 --- /dev/null +++ b/1325. Delete Leaves With a Given Value/1325. Delete Leaves With a Given Value.py @@ -0,0 +1,48 @@ +""" +Given a binary tree root and an integer target, delete all the leaf nodes with value target. + +Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot). + + + +Example 1: + + + +Input: root = [1,2,3,2,null,2,4], target = 2 +Output: [1,null,3,null,4] +Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left). +After removing, new nodes become leaf nodes with value (target = 2) (Picture in center). +Example 2: + + + +Input: root = [1,3,3,3,2], target = 3 +Output: [1,3,null,null,2] +Example 3: + + + +Input: root = [1,2,null,2,null,2], target = 2 +Output: [1] +Explanation: Leaf nodes in green with value (target = 2) are removed at each step. + + +Constraints: + +The number of nodes in the tree is in the range [1, 3000]. +1 <= Node.val, target <= 1000 +""" + + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def removeLeafNodes(self, root: Optional[TreeNode], target: int) -> Optional[TreeNode]: + if root.left: root.left = self.removeLeafNodes(root.left, target) + if root.right: root.right = self.removeLeafNodes(root.right, target) + return None if not root.left and not root.right and root.val == target else root diff --git a/3217. Delete Nodes From Linked List Present in Array/3217. Delete Nodes From Linked List Present in Array.py b/3217. Delete Nodes From Linked List Present in Array/3217. Delete Nodes From Linked List Present in Array.py new file mode 100644 index 0000000..b4b9fbc --- /dev/null +++ b/3217. Delete Nodes From Linked List Present in Array/3217. Delete Nodes From Linked List Present in Array.py @@ -0,0 +1,79 @@ +""" +You are given an array of integers nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums. + + + +Example 1: + +Input: nums = [1,2,3], head = [1,2,3,4,5] + +Output: [4,5] + +Explanation: + + + +Remove the nodes with values 1, 2, and 3. + +Example 2: + +Input: nums = [1], head = [1,2,1,2,1,2] + +Output: [2,2,2] + +Explanation: + + + +Remove the nodes with value 1. + +Example 3: + +Input: nums = [5], head = [1,2,3,4] + +Output: [1,2,3,4] + +Explanation: + + + +No node has value 5. +""" +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def modifiedList(self, nums, head): + hset = set(nums) + temp = ListNode(-100) + temp.next = head + result = temp + while temp.next: + if temp.next.val in hset: + temp.next = temp.next.next + else: + temp = temp.next + return result.next + +head = ListNode(1) +head.next = ListNode(2) +head.next.next = ListNode(3) +head.next.next.next = ListNode(4) +head.next.next.next.next = ListNode(5) + +# Sample input for nums +nums = [1, 2, 3] + +# Create an instance of the Solution class +solution = Solution() + +# Call the modifiedList method, passing both nums and head +result = solution.modifiedList(nums, head) + +# Print the modified linked list +while result: + print(result.val) + result = result.next \ No newline at end of file diff --git a/725. Split Linked List in Parts/725. Split Linked List in Parts.py b/725. Split Linked List in Parts/725. Split Linked List in Parts.py new file mode 100644 index 0000000..ef9ad10 --- /dev/null +++ b/725. Split Linked List in Parts/725. Split Linked List in Parts.py @@ -0,0 +1,63 @@ +""" +Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts. + +The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null. + +The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later. + +Return an array of the k parts. + + + +Example 1: + + +Input: head = [1,2,3], k = 5 +Output: [[1],[2],[3],[],[]] +Explanation: +The first element output[0] has output[0].val = 1, output[0].next = null. +The last element output[4] is null, but its string representation as a ListNode is []. +Example 2: + + +Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3 +Output: [[1,2,3,4],[5,6,7],[8,9,10]] +Explanation: +The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts. + + +Constraints: + +The number of nodes in the list is in the range [0, 1000]. +0 <= Node.val <= 1000 +1 <= k <= 50 +""" + + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def splitListToParts(self, head, k): + count, temp = 0, head + while temp: + temp = temp.next + count += 1 + chunk_size = count // k + extra_part = count % k + cur = head + result = [] + for i in range(k): + part_length = chunk_size + (1 if i < extra_part else 0) + part_head = cur + for j in range(part_length - 1): + if cur: + cur = cur.next + if cur: + next_part = cur.next + cur.next = None + cur = next_part + result.append(part_head) + return result