0ms Java solution


  • 0
    V

    /**

    • Definition for singly-linked list.

    • public class ListNode {

    • int val;
      
    • ListNode next;
      
    • ListNode(int x) { val = x; }
      
    • }
      */
      public class Solution {
      public ListNode reverseList(ListNode head) {

       if(head == null || head.next == null) {
           return head;
       }
       
       ListNode buffer = head.next;
       head.next = null;
       
       while(true) {
           if(buffer.next == null) {
               buffer.next = head;
               return buffer;
           }
           ListNode original = buffer.next;
           buffer.next = head;
           head = buffer;
           buffer = original;
       }
      

      }
      }


Log in to reply
 

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