My Accepted Solution


  • 0
    K

    I used an array list to reverse the linked list. The code is a little bit simpler.

    public void reorderList(ListNode head) 
    {
        if (head == null)
        {
            return;
        }
        
        ArrayList<ListNode> nodeList = new ArrayList<ListNode>();
        
        ListNode current = head;
        ListNode previous = head;
        
        while (current != null)
        {
            nodeList.add(current);
            previous = current;
            current = current.next;
        }
        
        int length = 0;
        int endIndex = nodeList.size() - 1;
        current = head;
        previous = head;
        
        while (length < nodeList.size() / 2)
        {
            ListNode tailNode = nodeList.get(endIndex);
            
            tailNode.next = current.next;
            current.next = tailNode;
            current = current.next.next;
            
            endIndex--;
            length++;
        }
        
        current.next = null;
    }

Log in to reply
 

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