Python concise solution, just 3 loops


  • 0
    #split linked list into two
    #reverse the second one
    #link them one by one
    class Solution(object):
        def reorderList(self, head):
            if not head:
                return None
            
            s = f = head1 = head 
            while f.next:
                if f.next.next:
                    s, f = s.next, f.next.next
                else:
                    f = f.next
            
            pre, s.next, head2 = s.next, None, None     
            while pre:
                pre.next, pre, head2 = head2, pre.next, pre
                
            while head2:
                head1.next, head1 = head2, head1.next
                head2.next, head2 = head1, head2.next
    

Log in to reply
 

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