Clear solution with stack JAVA


  • -2
    V
    public class Solution {
    public void reorderList(ListNode head) {
        if(head==null|)
            return;
        Stack<ListNode> stack=new Stack<ListNode>();
        ListNode node=head;
        while(node!=null){
            stack.push(node);
            node=node.next;
        }
        while(!stack.empty()&&stack.peek()!=head){
            ListNode next=head.next;
            ListNode nextNode=stack.pop();
            head.next=nextNode;
            if(nextNode==next){
                head=nextNode;
                break;
            }
            head=next;
            nextNode.next=head;
        }
        head.next=null;
        
    }
    

    }


  • 0
    L

    you used stack , so it is not in place algorithm
    it not in-place algorithm


Log in to reply
 

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