zoukankan      html  css  js  c++  java
  • [LeetCode#92]Reverse Linked List II

    The problem:

    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->NULLm = 2 and n = 4,

    return 1->4->3->2->5->NULL.

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

    My analysis:

    The mainipulation over Linkedlist involves many skills!!!
    1. proper use the dummy head to avoid the corner case over the first node. 
    ListNode dummy = new ListNode(1);
    dummy.next = head;
    ListNode pre = dummy;
    By using the dummy head, even the m is 1(which means the first node), we could have a pre pointer for any nodes in the linkedlist. Thus when "m = 1", we could still use the same invariant.
    
    2. How to move to the right node?
    Differ from the indexing method in array, requires us must start from the first node. the index method used in linkedlist is very flexible. Thus, we should understand very clear about the while loop and counting!!!
    Usually, we use following invariant of while loop:
    Assume the node temp is currently at the index m, if we need to reach the n node starting from it.
    1 int i = m;
    2 while (temp.next != null && i < n) {
    3    temp = temp.next;
    4    i++;
    5 }
    6 if (i < n) return not found
    Analysis:
    When we exit from the while loop(step 5), the current position is ith node. 
    Apparently, the checking condition "i < n" would be violated when i = n. that means the current i's value is n, indicates we are at the nth node!!!
    Note: we usually do other things in the while loop. 
    1 int i = m;
    2 while (temp.next != null && i < n) {
         other things ...
         ....
    3    temp = temp.next;
    4    i++;
    5 }
    6 if (i < n) return not found; //if (i < n) could use to check iff the linkedlist really have nth node.
    In this situation, we just reach the nth node, but the nth node doesn't enter into the while loop, so was not go through other things in the while loop.(the nth node was not processed). in order to let the nth node be processed, we need to change the loop condition into "while (temp.next != null && i < n + 1)" or "while (temp.next != null && i <= n)" .
    
    3.Clarification:
    while (i < n) {} seems not be able to reach the nth node?
    The i is actually use to indicate pre pointer's location(virtuallly). when the pre reached the nth node, it stop(notr proceed), thus only n-1 node poninted by the pre pointer enter the loop. What's more, since the we start from pre(pre = dummy), in fact only n-2 th node pointed by the prepointer enter the loop. But cur = pre.next.next. Thus, the cur actually reached the n+1th node, and nth node was the last node enter into the loop.  
    
    Those understanding are very important!!! You must master it!

    The solution:

    public class Solution {
        public ListNode reverseBetween(ListNode head, int m, int n) {
            if (head == null)
                return head;
            
            ListNode dummy = new ListNode(1);
            dummy.next = head;
            ListNode pre = dummy;
            ListNode cur;
            ListNode m_node;
            ListNode next;
            int i = 1;
            
            while (pre.next != null && i < m) {
                pre = pre.next;
                i++;
            }
            if (i < m)
                return head;
            
            m_node = pre.next;
            cur = m_node.next;
            
            while (i < n) {
                next = cur.next;
                cur.next = pre.next;
                pre.next = cur;
                m_node.next = next;
                cur = next;
                i++;
            }
            
            return dummy.next;
        }
    }
  • 相关阅读:
    HTML5-拖拽
    POJ1182食物链(并查集)
    欧拉函数之HDU1286找新朋友
    Another kind of Fibonacce(矩阵快速幂,HDU3306)
    我的第一道java_A+B
    bestcoder#33 1002 快速幂取余+模拟乘,组合数学
    快速幂模版
    bestcoder#33 1001 高精度模拟
    poj2446_二分图
    poj3984_bfs+回溯路径
  • 原文地址:https://www.cnblogs.com/airwindow/p/4227597.html
Copyright © 2011-2022 走看看