-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition_list.py
More file actions
36 lines (36 loc) · 1.06 KB
/
Copy pathpartition_list.py
File metadata and controls
36 lines (36 loc) · 1.06 KB
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]:
t = head
tempHead = None
tempTail = None
NextIter = []
while t:
if t.val < x:
if tempHead == None:
tempHead= ListNode(t.val)
tempTail = tempHead
else:
l = ListNode(t.val)
tempTail.next = l
tempTail = l
else:
NextIter.append(t.val)
t = t.next
if x == 0 and NextIter == []:
return head
while NextIter:
l = ListNode(NextIter[0])
if tempTail:
tempTail.next = l
tempTail = l
NextIter.pop(0)
else:
tempHead = l
tempTail = l
NextIter.pop(0)
return tempHead