-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0086_Partition_List.py
36 lines (31 loc) · 958 Bytes
/
0086_Partition_List.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
# 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: Optional[ListNode], x: int) -> Optional[ListNode]:
f_head = ListNode(val = -101)
b_head = ListNode(val = -101)
f_pointer = f_head
b_pointer = b_head
while head:
if not head:
break
val = head.val
if val >= x:
b_pointer.next = head
b_pointer = b_pointer.next
else:
f_pointer.next = head
f_pointer = f_pointer.next
head = head.next
sol = None
if f_head.next:
sol = f_head.next
else:
return b_head.next
if b_head.next:
f_pointer.next = b_head.next
b_pointer.next = None
return sol