My easy understanding Java solution using stack


  • -1
    Y
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;
        Stack<Integer> stack = new Stack<Integer>();
        while(head.next != null){
            stack.push(head.val);
            head = head.next;
        }
        ListNode node = new ListNode(stack.pop());
        head.next = node;
        while(!stack.isEmpty()){
            node.next = new ListNode(stack.pop());
            node = node.next;
        }
        return head;
    }

Log in to reply
 

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