2014年2月14日星期五

LeetCode - Send Feedback Reverse Linked List II

 Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.

Note:
Given m, n satisfy the following condition:
1 ≤ mn ≤ length of list.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if(head==null) {
            return null;
        }
        ListNode p0 = null;
        ListNode p1 = head;
        ListNode p2 = head;
        while(n-->1) {
            if(m-->1) {
                p1 = p1.next;
                if(p0==null) {
                    p0 = head;
                } else {
                    p0 = p0.next;
                }
            }
            p2 = p2.next;
        }
        ListNode p3 = p2.next;
        //reverse p1 to p2;
        ListNode cur1 = p1;
        ListNode cur2 = cur1.next;
   while(p1!=p2 && cur1!=null && cur1.next!=null) {
            ListNode cur3 = cur2.next;
            if(cur1==p1) {
                cur1.next = p3;
            }
            cur2.next = cur1;
            if(cur2==p2) {
                break;
            }
            cur1 = cur2;
            cur2 = cur3;
        }
        if(p0!=null) {
            p0.next = p2;
            return head;
        } else {
            return p2;
        }
    }
}


public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if(head==null || head.next==null || m==n) return head;
       
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode p0 = null;
       
        ListNode p1 = dummy;
        n = n-m;
        while(m>0) {
            if(p0==null) p0 = dummy;
            else p0 = p1;
            p1 = p1.next;
            m--;
        }
       
        ListNode newTail = p1;
        ListNode p2 = p1.next;
        ListNode tmp = null;
        while(true) {
            //1->2->3->4->5->NULL
            tmp = p2.next;
            p2.next = p1;
            n--;
            if(n==0) break;
            p1 = p2;
            p2 = tmp;
        }
       
        p0.next = p2;
        newTail.next = tmp;
       
        return dummy.next;
    }
}


=========

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if (head == null || head.next == null || m == n) {
            return head;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode p = dummy;
        int tmp = m;
        while (tmp-- > 1) {
            p = p.next;
        }
        ListNode preReverseHead = p;
        ListNode reverseHead = p.next;
       
        ListNode pre = p.next;
        p = reverseHead.next;
        int len = n - m + 1;
        while (len-- > 1) {
            ListNode nextP = p.next;
            p.next = pre;
           
            pre = p;
            p = nextP;
        }
        preReverseHead.next = pre;
        reverseHead.next = p;
        return dummy.next;
    }
}

没有评论:

发表评论