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;
        }
    }
  • 相关阅读:
    dotnet 读 WPF 源代码笔记 了解 WPF 已知问题 用户设备上不存在 Arial 字体将导致应用闪退
    WPF 基础 2D 图形学知识 判断点是否在线段上
    WPF 实现自定义的笔迹橡皮擦
    WPF 加载诡异的字体无法布局
    dotnet 5 从 IL 层面分析协变返回类型新特性
    WPF 笔迹触摸点收集工具
    dotnet C# 序列化 XML 时进行自动格式化
    自定义systemctl服务
    maven之三大特性属性、profile、资源过滤
    dependencyManagement与dependencies
  • 原文地址:https://www.cnblogs.com/airwindow/p/4227597.html
Copyright © 2011-2022 走看看