Title Description
A list is given. Each k nodes are flipped and the flipped list is returned. K is a positive integer whose value is less than or equal to the length of the list. If the total number of nodes is not an integral multiple of K, the remaining nodes will remain in the original order.
Explain:
- You need to define your own linked list structure and save the input data to your linked list;
- You can't just change the internal values of nodes, but you need to actually exchange nodes;
- Your algorithm can only use constant extra space.
Input description
The first line of input is the value of the linked list
The second line of input is the value of K, which is an integer greater than or equal to 1
The input form is:
1 2 3 4 5
2
Output description
When k = 2, it should output:
2 1 4 3 5
When k = 3, it should output:
3 2 1 4 5
When k=6, it should output:
1 2 3 4 5
Example
input
1 2 3 4 5
2
output
2 1 4 3 5
Title Analysis
I read the problem wrong at the beginning and it has been unable to pass. I think it is k nodes before inversion. The topic describes that each K node is a group for inversion. So it is divided into the following steps:
- The list is divided into n/k groups with k nodes in each group;
- Each group was reversed internally;
- The inverted segmented list with k nodes is connected;
So the hardest part is the connection between the inversion function and the inversion.
4. The reverse function is as follows:
ListNode pre=null; ListNode next=null; while(pre!=tail) { next=head.next; head.next=pre; pre=head; head=next; }
- Connect as shown in the figure
This picture is from fly ABCD Zhang, which describes the connection process very well.
Code
The Java code is as follows:
import java.util.Scanner; public class Main { //Define Node node static class ListNode { public int val; public ListNode next =null; public ListNode(int val) { this.val = val; } } public static void main(String[] args) { //1. Get input information Scanner scanner=new Scanner(System.in); String string =scanner.nextLine(); int k=scanner.nextInt(); String[] strings=string.split(" "); //2. Create a header node ListNode head=new ListNode(0); ListNode tail=head; //3. Change the input string into a linked list node for(String str : strings) { ListNode newNode=new ListNode(Integer.valueOf(str)); tail.next=newNode; tail=tail.next; } head=head.next; //Reverse list per k ListNode node =reverseGroup(head,k); while(node!=null) { System.out.print(node.val+" "); node=node.next; } } //Continuously take k pieces for turning, if not enough, return directly, end public static ListNode reverseGroup(ListNode head, int k) { if(head==null||head.next==null||k<=1) return head; ListNode currentNode=head; //Get the first and last nodes of k elements for(int count =1;count<k;count++) { currentNode=currentNode.next; //If it's not enough, go straight back if(currentNode==null) return head; } ListNode next=currentNode.next; //Reverse local list reverse(head,currentNode); head.next=reverseGroup(next,k); return currentNode; } //Write a local function with the head and tail node reversed public static ListNode reverse(ListNode head, ListNode tail) { if(head==null||head.next==null) return head; ListNode pre=null; ListNode next=null; while(pre!=tail) { next=head.next; head.next=pre; pre=head; head=next; } return pre; } }