-
Notifications
You must be signed in to change notification settings - Fork 1
/
Partition List
46 lines (45 loc) · 1.33 KB
/
Partition List
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
44
45
46
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
if(head is None):
return None
elif(head.next==None):
return head
else:
first=head
y=None
xx=None
curx=None
sx=None
last=None
while(first is not None):
if(first.val<x):
if(xx is None):
xx=ListNode(first.val)
xx.next=None
curx=xx
else:
sx=ListNode(first.val)
curx.next=sx
curx=sx
sx=None
else:
if(y is None):
y=ListNode(first.val)
y.next=None
cury=y
else:
sy=ListNode(first.val)
cury.next=sy
cury=sy
sy=None
first=first.next
if(curx!=None):
curx.next=y
return xx
else:
return y