Java 2ms short solution


  • 0
    L
    public void reorderList(ListNode head) {
        ListNode bNode = head, fNode = head;
        while(fNode != null && fNode.next != null){
            fNode = fNode.next.next;
            bNode = bNode.next;
        }
        if(bNode == null || bNode.next == null) return;
        fNode = bNode.next;
        while(fNode != null && fNode.next != null){
            ListNode tmp = fNode.next;
            fNode.next = tmp.next;
            tmp.next = bNode.next;
            bNode.next = tmp;
        }
        fNode = bNode.next;
        bNode.next = null;
        bNode = head;
        while(fNode != null){
            ListNode tmp = fNode.next;
            fNode.next = bNode.next;
            bNode.next = fNode;
            bNode = fNode.next;
            fNode = tmp;
        }
    }

Log in to reply
 

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