Skip to content

Commit a0b5e2f

Browse files
authored
Merge pull request #12 from 149ps/LC-problems
LC Problems
2 parents c18874f + 7639dd1 commit a0b5e2f

File tree

4 files changed

+194
-7
lines changed

4 files changed

+194
-7
lines changed

116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ def __init__(self, val, left, right, next):
2828
self.next = next
2929
"""
3030
class Solution:
31-
def connect(self, root: 'Node') -> 'Node':
32-
if not root:
33-
return None
34-
if root.left:
35-
root.left.next = root.right
31+
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
32+
if not root: return None
3633
if root.right:
34+
if root.left:
35+
root.left.next = root.right
3736
if root.next:
3837
root.right.next = root.next.left
39-
else:
40-
root.right.next = None
4138
self.connect(root.left)
4239
self.connect(root.right)
4340
return root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Given a binary tree root and an integer target, delete all the leaf nodes with value target.
3+
4+
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).
5+
6+
7+
8+
Example 1:
9+
10+
11+
12+
Input: root = [1,2,3,2,null,2,4], target = 2
13+
Output: [1,null,3,null,4]
14+
Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left).
15+
After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
16+
Example 2:
17+
18+
19+
20+
Input: root = [1,3,3,3,2], target = 3
21+
Output: [1,3,null,null,2]
22+
Example 3:
23+
24+
25+
26+
Input: root = [1,2,null,2,null,2], target = 2
27+
Output: [1]
28+
Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
29+
30+
31+
Constraints:
32+
33+
The number of nodes in the tree is in the range [1, 3000].
34+
1 <= Node.val, target <= 1000
35+
"""
36+
37+
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, val=0, left=None, right=None):
41+
# self.val = val
42+
# self.left = left
43+
# self.right = right
44+
class Solution:
45+
def removeLeafNodes(self, root: Optional[TreeNode], target: int) -> Optional[TreeNode]:
46+
if root.left: root.left = self.removeLeafNodes(root.left, target)
47+
if root.right: root.right = self.removeLeafNodes(root.right, target)
48+
return None if not root.left and not root.right and root.val == target else root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
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.
3+
4+
5+
6+
Example 1:
7+
8+
Input: nums = [1,2,3], head = [1,2,3,4,5]
9+
10+
Output: [4,5]
11+
12+
Explanation:
13+
14+
15+
16+
Remove the nodes with values 1, 2, and 3.
17+
18+
Example 2:
19+
20+
Input: nums = [1], head = [1,2,1,2,1,2]
21+
22+
Output: [2,2,2]
23+
24+
Explanation:
25+
26+
27+
28+
Remove the nodes with value 1.
29+
30+
Example 3:
31+
32+
Input: nums = [5], head = [1,2,3,4]
33+
34+
Output: [1,2,3,4]
35+
36+
Explanation:
37+
38+
39+
40+
No node has value 5.
41+
"""
42+
# Definition for singly-linked list.
43+
class ListNode:
44+
def __init__(self, val=0, next=None):
45+
self.val = val
46+
self.next = next
47+
48+
class Solution:
49+
def modifiedList(self, nums, head):
50+
hset = set(nums)
51+
temp = ListNode(-100)
52+
temp.next = head
53+
result = temp
54+
while temp.next:
55+
if temp.next.val in hset:
56+
temp.next = temp.next.next
57+
else:
58+
temp = temp.next
59+
return result.next
60+
61+
head = ListNode(1)
62+
head.next = ListNode(2)
63+
head.next.next = ListNode(3)
64+
head.next.next.next = ListNode(4)
65+
head.next.next.next.next = ListNode(5)
66+
67+
# Sample input for nums
68+
nums = [1, 2, 3]
69+
70+
# Create an instance of the Solution class
71+
solution = Solution()
72+
73+
# Call the modifiedList method, passing both nums and head
74+
result = solution.modifiedList(nums, head)
75+
76+
# Print the modified linked list
77+
while result:
78+
print(result.val)
79+
result = result.next
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts.
3+
4+
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.
5+
6+
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.
7+
8+
Return an array of the k parts.
9+
10+
11+
12+
Example 1:
13+
14+
15+
Input: head = [1,2,3], k = 5
16+
Output: [[1],[2],[3],[],[]]
17+
Explanation:
18+
The first element output[0] has output[0].val = 1, output[0].next = null.
19+
The last element output[4] is null, but its string representation as a ListNode is [].
20+
Example 2:
21+
22+
23+
Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
24+
Output: [[1,2,3,4],[5,6,7],[8,9,10]]
25+
Explanation:
26+
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.
27+
28+
29+
Constraints:
30+
31+
The number of nodes in the list is in the range [0, 1000].
32+
0 <= Node.val <= 1000
33+
1 <= k <= 50
34+
"""
35+
36+
37+
# Definition for singly-linked list.
38+
# class ListNode:
39+
# def __init__(self, val=0, next=None):
40+
# self.val = val
41+
# self.next = next
42+
class Solution:
43+
def splitListToParts(self, head, k):
44+
count, temp = 0, head
45+
while temp:
46+
temp = temp.next
47+
count += 1
48+
chunk_size = count // k
49+
extra_part = count % k
50+
cur = head
51+
result = []
52+
for i in range(k):
53+
part_length = chunk_size + (1 if i < extra_part else 0)
54+
part_head = cur
55+
for j in range(part_length - 1):
56+
if cur:
57+
cur = cur.next
58+
if cur:
59+
next_part = cur.next
60+
cur.next = None
61+
cur = next_part
62+
result.append(part_head)
63+
return result

0 commit comments

Comments
 (0)