JAVA 0ms Solution, no recursive


  • 0
    E
    public class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode h = new ListNode(0);
            ListNode tmp = new ListNode(0);
            ListNode prev = new ListNode(0);
            tmp.next = head;
            prev.next = head;
            h.next = tmp;
            ListNode p = new ListNode(0);
            while (tmp.next != null) {
                p.next = tmp.next;
                tmp.next = tmp.next.next;
                p.next.next = h.next;
                h.next = p.next;
                p.next = null;
            }
            prev.next.next = null;
            return h.next;
        }
    }

Log in to reply
 

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