Clean python code with Dummy Node


  • 0
    class Solution(object):
        def partition(self, head, x):
            dummyLHead = dummyLTail = ListNode(0)
            dummyGHead = dummyGTail = ListNode(0)
    
            while head:
                if head.val < x:
                    dummyLTail.next, dummyLTail = head, head
                else:
                    dummyGTail.next, dummyGTail = head, head
                head = head.next
    
            dummyLTail.next, dummyGTail.next = dummyGHead.next, None
            return dummyLHead.next

  • 0
    A

    Hi, shiyanhui,
    I am a beginner of python. Could you tell me what is 'while head:' means here ? Does that mean when the head is not None, do the while loop ?


  • 0
    A

    If I may, you are right.
    While head, would be true while head is Not None


Log in to reply
 

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