Simple Python Solution and Easy to Understand


  • 2
    def partition(self, head, x):
        if not head:
            return None
        dy = ListNode(0)
        dy.next = head
        a = dy
        while a.next and a.next.val < x:
            a = a.next
        b = a.next
        c = b
        while c and c.next:
            if c.next.val < x:
                d = c.next
                c.next = c.next.next
                a.next = d
                d.next = b
                a = d
            else:
                c = c.next
        return dy.next

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.