Share my simple JAVA solution


  • 0
    W
    public class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode preHead = new ListNode(0);
            preHead.next = head;
            ListNode tail = head;
            ListNode runner = head.next;
            while (runner != null) {
                ListNode tmp1 = preHead.next;
                ListNode tmp2 = runner.next;
                preHead.next = runner;
                runner.next = tmp1;
                runner = tmp2;
            }
            tail.next = null;
            return preHead.next;
        }
    }

Log in to reply
 

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