My accepted java code with O(n)


  • -1
    H
    public void reorderList(ListNode head) {
    	if(head == null)
    		return;
    	ArrayList<ListNode> arrayNode = new ArrayList<ListNode>();
    	for(ListNode node=head; node!=null; node=node.next)
    	{
    		arrayNode.add(node);
    	}
    	int i = 0, j = arrayNode.size()-1;
    	for(; i < j; ++i,--j)
    	{
    		ListNode previous = arrayNode.get(i);
    		ListNode later = arrayNode.get(j);
    		previous.next = later;
    		later.next = arrayNode.get(i+1);
    	}
    		arrayNode.get(i).next=null;
    }

  • 0
    S

    Your solution is not "in-place".


  • 0
    M

    the requirement is in-place....it's easy when use extra collections...


Log in to reply
 

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