Java iterative solution


  • 0
    A
    public class Solution {
     public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode firstNode = new ListNode(head.val);
        firstNode.next = head.next;
        head.next = null;
        ListNode temp = firstNode;
        while(temp.next != null) {
            firstNode = temp.next;
            temp = new ListNode(firstNode.val);
            temp.next = firstNode.next;
            firstNode.next = head;
            head = firstNode;
        }
    
        return firstNode;
    }
    

    }


Log in to reply
 

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