Python 3 solution using 2 linked lists

programming
Tags: #<Tag:0x00007f2425634020>

#1
class Solution:
    # @param A : head node of linked list
    # @param B : integer
    # @return the head node in the linked list
    def partition(self, A, B):
        
        less = ListNode(0)    #for storing nodes having values less than B
        head = less
        more = ListNode(0)      #for storing nodes having values greater than equal to B
        morehead = more
        
        while A:
            x = ListNode(A.val)
            if A.val<B:
                less.next = x
                less = less.next
            else:
                more.next = x
                more = more.next
            A = A.next
            
        less.next = morehead.next   #joining both linked lists
        return head.next