[List] LeetCode reverse and its ramifications

(A) basis: inversion problem list

  See: [15] to prove safety Offer, reverse list , 206. The reverse list

  (1) Three pointer. Using three pointers, pointing to the currently traversed node, its previous node, and after a node. After the pointer is reversed to three nodes sequentially forward. (2) a recursive method. The same can be achieved using reverse recursion. After the list of the first node after inversion, to the tail and then the head node can.

    //方法一:三指针
    public ListNode ReverseList(ListNode head) {
        if(head==null)
            return null;
        ListNode first=null; 
        ListNode second=head;
        ListNode third=head.next;
        while(third!=null){ 
            second.next=first; //三指针之间的变换
            first=second;
            second=third;
            third=third.next;
        }
        second.next=first;
        return second;
    }

    //方法二:递归
    public ListNode ReverseList(ListNode head) {
         if(head==null||head.next==null)
            return head;
        ListNode temp=ReverseList(head.next);
        head.next.next=head;
        head.next=null;
        return temp;   
    }

(Ii) reverse list II (from m to n)

Title (Medium): 92. II reverse list

Subject description:

Reverse list from the position m to n. Please use the trip to the scan is complete reversal.

Description:
. 1 ≤ ≤ m ≤ n-length list.

示例:

输入: 1->2->3->4->5->NULL, m = 2, n = 4

输出: 1->4->3->2->5->NULL

Problem-solving ideas:

  This is a question of extending inverted list, in fact, as long as a clear rationale for each change pointer, not difficult to achieve. As shown below,

  Step 1: Find a node before the node to be reversed.

  Step: Reverse m to n this part.

  The third step: the starting point behind the reversal of the reversal of a portion of the next point.

  Step 4: After the first step in finding a node points to reverse the head node.



Photo: Author: reedfan
link: https://leetcode-cn.com/problems/reverse-linked-list-ii/solution/ji-bai-liao-100de-javayong-hu-by-reedfan-6/
Source: stay button (LeetCode)
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Code:

class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if(head==null || head.next==null)
            return head;
        ListNode phead=new ListNode(-1); //头结点
        phead.next=head;
        ListNode firstTail=null,cur=phead;
        //走m步,则cur是第m个结点,pre是反转之前最后一个,也就是正常一段的末尾
        for(int i=0;i<m;i++){
            firstTail=cur;
            cur=cur.next;
        }
        ListNode reverseTail=cur;
        //三指针法,反转从m到n的这一段
        ListNode pre=null,third=cur.next;
        for(int i=m;i<=n;i++){
            cur.next=pre;
            pre=cur;
            cur=third;
            if(third!=null)
                third=third.next;
        }
        reverseTail.next=cur;
        firstTail.next=pre;
        return phead.next;
    }
}

(C) K inverted list in groups

Title (Hard): 25. A group of inverted lists a number K

Subject description:

Give you a list, each k nodes to flip a group, you return to the list after the flip.

k is a positive integer that is less than or equal to the length of the list.

If the total number of nodes is not an integral multiple of k, then set the last remaining node original order.

示例 :

给定这个链表:1->2->3->4->5

当 k = 2 时,应当返回: 2->1->4->3->5

当 k = 3 时,应当返回: 3->2->1->4->5

Description:

You can only use the extra space algorithm constants.
You can not just simply change the internal node values, but need to be the actual node exchange.


Problem-solving ideas:

  This problem is relatively hard to reflect further on the basis of the first two questions, the key is to change and move the pointer to be understood clearly. Mainly by the following figure shows:



Photo: Author: deppwang-2
Links: https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/25-k-ge-yi-zu-fan-zhuan-lian- biao-by-deppwang-2 /
source: stay button (LeetCode)
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Code:

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(head==null || head.next==null)
            return head;
        ListNode phead=new ListNode(-1);
        phead.next=head;
        ListNode pre=phead,end=phead;
        while(end.next!=null){
            for(int i=0;i<k;i++){
                if(end!=null)
                    end=end.next;
            }
            if(end==null)
                break;
            ListNode temp=end.next; //记录下一次的起点
            end.next=null; //断开
            ListNode start=pre.next;
            pre.next=reverse(start); //反转
            
            start.next=temp;
            pre=start;
            end=start;
        }
        return phead.next;
    }
    //链表反转
    public ListNode reverse(ListNode head){
        if(head==null || head.next==null)
            return head;
        ListNode first=null,second=head,third=head.next;
        while(third!=null){
            second.next=first;
            first=second;
            second=third;
            third=third.next;
        }
        second.next=first;
        return second;
    }
}

to sum up:

  In the above three questions inverted list is based, progressive layers, is a typical problem objectlist title, the main core is a clear rationale for each change of the pointer, while use should always pay attention to prevent the first node, and null pointer.

Guess you like

Origin www.cnblogs.com/gzshan/p/12399239.html