Skip to main content

86. Partition List

Link

# 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]:

dummy = ListNode(-1)
greaterDummy = ListNode(-1)

current = head
c2 = dummy
c3 = greaterDummy

while current:

if current.val < x:
c2.next = current
c2 = c2.next
else:
c3.next = current
c3 = c3.next

current = current.next

c2.next = greaterDummy.next
c3.next = None

return dummy.next