Python solution


  • 3
    F

    Does't matter if the length of the list is odd or even, find mid, split, reverse the second list and insert. Pretty straightfowward.

    class Solution(object):
    def reorderList(self, head):
        if not head or not head.next or not head.next.next: return
        slow, fast=head, head
        while fast.next and fast.next.next: slow, fast=slow.next, fast.next.next
        head1, head2=head, slow.next
        slow.next, cur, pre=None, head2, None
        while cur:
            curnext=cur.next
            cur.next=pre
            pre=cur
            cur=curnext
        cur1, cur2=head1, pre
        while cur2:
            next1, next2=cur1.next, cur2.next
            cur1.next=cur2
            cur2.next=next1
            cur1, cur2=next1, next2

  • 0
    G

    Great solution. Best part is it is O(n)


Log in to reply
 

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