Python solution with detailed explanation

  • 0


    Partition List

    1. Use dummy node to create two lists for less than and greater than. Then join them.
    2. Use an example to run through this code.
    class Solution(object):
        def partition(self, head, x):
            :type head: ListNode
            :type x: int
            :rtype: ListNode
            if head == None:
                return None
            dummy_before = d_temp1 = ListNode(-1)
            dummy_after = d_temp2 = ListNode(-1)
            while head:
                if head.val < x:
           = head
                    d_temp1 = head
           = head
                    d_temp2 = head
                head =
   = None

Log in to reply

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