JAVA-----------Easy Version To UnderStand!!!!!


  • 0
    H
    	public static void reorderList(ListNode head) {
    	if (head == null || head.next == null || head.next.next == null)
    		return;
    	ListNode fast = head, slow = head;
    	while (fast.next != null && fast.next.next != null) {
    		fast = fast.next.next;
    		slow = slow.next;
    	}
    	ListNode h1 = new ListNode(0), h2 = new ListNode(0);
    	h1.next = null;
    	h2.next = null;
    	ListNode p = slow.next, tmp, r = h2, q;
    	slow.next = null;
    	while (p != null) {
    		tmp = p.next;
    		p.next = h1.next;
    		h1.next = p;
    		p = tmp;
    	}
    	p = head;
    	q = h1.next;
    	while (p != null && q != null) {
    		r.next = p;
    		p = p.next;
    		r = r.next;
    		r.next = q;
    		q = q.next;
    		r = r.next;
    	}
    	if (p != null) {
    		r.next = p;
    		r = r.next;
    	}
    	r.next = null;
    	head = h2.next;
    }

Log in to reply
 

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