Leetcode problem solution 25-K a set of flip linked list

Problem Description

Give you a linked list, every k nodes are flipped, please return to the flipped linked list.

k is a positive integer, and its value is less than or equal to the length of the linked list.

If the total number of nodes is not an integral multiple of k, please keep the last remaining nodes in the original order.

Advanced:

Can you design an algorithm that uses only constant extra space to solve this problem?
You can't just change the value inside the node, but need to actually exchange the node.

Example 1:
Insert picture description here

输入:head = [1,2,3,4,5], k = 2
输出:[2,1,4,3,5]

Example 2:
Insert picture description here

输入:head = [1,2,3,4,5], k = 3
输出:[3,2,1,4,5]

Example 3:

输入:head = [1,2,3,4,5], k = 1
输出:[1,2,3,4,5]

Example 4:

输入:head = [1], k = 1
输出:[1]

Problem-solving ideas

1. Count the length
of the linked list 2. Calculate the number of times the linked list flips reverse_times=len/k;

  • If the number of flips is 0, simply return to the original linked list.

3. Define multiple variables

  • cur_time saves the current number of flips (can be used to determine the number of flips)
  • nhead virtual head node (a sentinel head node that does not contain data can be defined in general linked list questions)
  • knext saves the linked list head node of the next flip (to prevent the loss of subsequent linked list data)
  • The head node of the partial linked list that cur_head currently needs to be flipped is actually the previous node of the head node (which is convenient for partial flipping).
  • The tail node of the partial linked list currently flipped by cur_tail is used to connect with knext after the flip is completed to ensure that data will not be lost.

4. Perform the flip of the linked list

  • 1. Point knext to the head node of the linked list at the next flip
  • 2. Use the head interpolation method to reverse (note that you need to use cur_tail to save the first node of the local linked list. After the flip is completed, it will be in the last position of the local linked list, and then connect it with the following linked list)
  • 3. Re-assign the tail node to the head node, and let the next pointer of the tail node point to knext
  • 4. Number of flips +1
    5. Return to next.next;
class Solution {
    
    
    public ListNode reverseKGroup(ListNode head, int k) {
    
    
        int len=0;                  //统计链表的长度
        ListNode plen=head;
        while(plen!=null){
    
    
            len++;
            plen=plen.next;
        }

        int reverse_times=len/k;     //计算链表的翻转次数
        if(k<=1 || reverse_times==0){
    
    
            return head;
        }

        ListNode knext=head;         //保存下一次翻转时候的链表头结点
        int cur_times=0;      //保存当前翻转次数
        ListNode nhead=new ListNode(-1);    //虚拟头结点,不包含任何数据
        ListNode cur_head=nhead;
        cur_head.next=head;
        ListNode cur_tail=null;
        while(cur_times<reverse_times){
    
    
            //将knext指向下一次翻转时候的链表头结点
            for(int i=0;i<k;i++){
    
    
                knext=knext.next;
            }

            //使用头插法进行反转            
            ListNode p=cur_head.next;        //保存当前需要头插的结点
            cur_tail=cur_head.next;
            ListNode next=null;         //保存反转时下一个结点的指针
            for(int i=0;i<k;i++){
    
    
                next=p.next;           
                p.next=cur_head.next;
                cur_head.next=p;
                p=next;
            }
            //将尾部结点重新赋值给头部结点,并且让尾部结点的next指针指向knext
            cur_head=cur_tail;
            cur_tail.next=knext;
            // System.out.println("3 "+knext.val);
            cur_times++;
        }        
        return nhead.next;
    }
}

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/115029580