Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7639dd1

Browse files
committed
3217-> O(n) time and O(n) space using hashset
1 parent 6b95d67 commit 7639dd1

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 79 additions & 0 deletions
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
Lines changed: 63 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /