Java simple iterative solution


  • 0
    M
    public class Solution {
    
      public ListNode reverseList(ListNode head) {
          
          if (head == null) return null;
          
          ListNode prev = null;
          ListNode cur = head;
          ListNode next = head.next;
          
          while (cur != null) {
              cur.next = prev;
              prev = cur;
              cur = next;
              next = next != null ? next.next : null;
          }      
          return prev;
        }    
    }
    

Log in to reply
 

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