forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartition List.py
33 lines (29 loc) · 857 Bytes
/
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
# Runtime: 53 ms (Top 53.47%) | Memory: 13.8 MB (Top 76.51%)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
"""
lessthan = []
greateql = []
while head:
if head.val < x:
lessthan.append(head.val)
else:
greateql.append(head.val)
head = head.next
h = res = ListNode()
for i in range(len(lessthan)):
res.next = ListNode(lessthan[i])
res = res.next
for i in range(len(greateql)):
res.next = ListNode(greateql[i])
res = res.next
return h.next