-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0086-partition-list.py
More file actions
27 lines (22 loc) · 751 Bytes
/
0086-partition-list.py
File metadata and controls
27 lines (22 loc) · 751 Bytes
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
# time complexity: O(n)
# space complexity: O(n)
from typing import Optional
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]:
lessNode, greatNode = ListNode(0), ListNode(0)
lessHead, greatHead = lessNode, greatNode
while head != None:
if head.val < x:
lessNode.next = head
lessNode = lessNode.next
else:
greatNode.next = head
greatNode = greatNode.next
head = head.next
greatNode.next = None
lessNode.next = greatHead.next
return lessHead.next