-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition_List.py
More file actions
27 lines (22 loc) · 806 Bytes
/
Partition_List.py
File metadata and controls
27 lines (22 loc) · 806 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
# 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]:
less = ListNode(0)
less_end = less #holds pointer to last element of less list for appending
greater = ListNode(0)
greater_end = greater
while head != None:
if head.val >= x: #greater than x
greater_end.next = head
greater_end = greater_end.next
else: #less than x
less_end.next = head
less_end = less_end.next
head = head.next
greater_end.next = None #terminates list to avoid a cycle
less_end.next = greater.next #conjoins lists
return less.next #final list