-
Notifications
You must be signed in to change notification settings - Fork 26
/
86.PartitionList.py
43 lines (38 loc) · 1.23 KB
/
86.PartitionList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Given a linked list and a value x, partition it such that all nodes less
than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of
the two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
"""
#Difficulty: Medium
#166 / 166 test cases passed.
#Runtime: 36 ms
#Memory Usage: 14.2 MB
#Runtime: 36 ms, faster than 57.43% of Python3 online submissions for Partition List.
#Memory Usage: 14.2 MB, less than 100.00% of Python3 online submissions for Partition List.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
left = [0]
right = []
curr = head
while curr:
if curr.val < x:
left.append(curr.val)
else:
right.append(curr.val)
curr = curr.next
left.extend(right)
head = ListNode(left.pop(0))
node = head
while left:
node.next = ListNode(left.pop(0))
node = node.next
return head.next