2014年2月13日星期四

LeetCoder - Reorder List

 Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-2→…
You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) {
return;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
fast = slow;
slow = slow.next;
fast.next = null;

// reverse fast link
ListNode p1 = slow;
ListNode p2 = slow.next;
while (p2 != null) {
slow = p2.next;
p2.next = p1;
if(p1.next==p2) {
p1.next = null;
}
p1 = p2;
p2 = slow;
}

// combine
ListNode cur = head;
ListNode cur2 = head.next;

p2 = p1.next;

while (true) {
cur.next = p1;
p1.next = cur2;

if(cur2==null || p2==null) {
break;
}
cur = cur2;
cur2 = cur2.next;
p1 = p2;
p2 = p2.next;
}
}
}



public class Solution {
    public void reorderList(ListNode head) {
        if(head==null || head.next==null || head.next.next==null) return;
        ListNode p1 = head;
        ListNode p2 = head;
        while(p2.next!=null && p2.next.next!=null) {
            p1 = p1.next;
            p2 = p2.next.next;
        }
        ListNode half = p1.next;
        p1.next = null;
       
        // reverse half
        p1 = half;
        p2 = p1.next;
       
        while(p2!=null) {
            ListNode tmp = p2.next;
            p2.next = p1;
            p1 = p2;
            p2 = tmp;
        }
        half.next = null;
        // combine
        ListNode head2 = p1;
        ListNode p0 = head;
        p1 = head.next;
        p2 = head2;
        while(p1!=null || p2!=null) {
            if(p2!=null) {
                p0.next = p2;
                p2 = p2.next;
                p0 = p0.next;
            }
            if(p1!=null) {
                p0.next = p1;
                p1 = p1.next;
                p0 = p0.next;
            }
        }
    }
}

==========

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void reorderList(ListNode head) {
        if (head == null || head.next == null) {
            return;
        }
        ListNode p1 = head;
        ListNode p2 = p1.next;
        while (p2 != null && p2.next != null) {
            p1 = p1.next;
            p2 = p2.next.next;
        }
       
        ListNode head2 = p1.next;
        p1.next = null;
       
        // reverse head2
        ListNode pre = null;
        p1 = head2;
        while (p1 != null) {
            ListNode nextP1 = p1.next;
            p1.next = pre;
           
            pre = p1;
            p1 = nextP1;
        }
        ListNode newHead2 = pre;
        ListNode p = head;
        p1 = head.next;
        p2 = newHead2;
       
        while (p1 != null || p2 != null) {
            if (p2 != null) {
                p.next = p2;
                p2 = p2.next;
            }
            p = p.next;
            if (p1 != null) {
                p.next = p1;
                p1 = p1.next;
            }
            p = p.next;
        }
    }
   
   
}

没有评论:

发表评论