"025-reverse Nodes in K-group (a set of K nodes in a single linked list is reversed)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Given A linked list, reverse the nodes of a linked list K at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, and only nodes itself is changed.
Only constant memory is allowed.
For example,
Given This linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Main Topic
Given a single-linked list, and a group of K, each K-node is reversed, if the last node is less than k to keep the original link order unchanged.
Thinking of solving problems
Record the last node of the linked list (tail) with a pointer, record the last node (head) of the linked part with a pointer to the node at the head of the non-linked list, insert the K element, and then move head to tail, tail re-record the tail node of the linked list Until all the elements have been manipulated. If the last set of elements is less than k, because the tail interpolation method, so also to restore, the final head element of the tail interpolation method can be
Code Implementation
Node class
publicclass ListNode { int val; ListNode next; ListNode(int x) { val = x; }}
Algorithm implementation class
Public class solution { PublicListNode Reversekgroup (ListNode head,intK) {if(k <=1) {returnHead } ListNode root =NewListNode (0);//The precursor of the first element of a groupListNode grouphead = root;//Current node to be processedListNode Curr = head;///processed tail node of the linked listListNode grouptail = head;//The successor of the node currently to be processedListNode Next;//How many nodes are processed for each group int Count=0; while(Curr! =NULL) {//If it is the first element of a group, record it if(Count==0) {grouptail = Curr; }//record number of elements processed Count++;//Record the next node to be processed.Next = Curr.next;//operation by using the tail interpolation methodCurr.next = Grouphead.next; Grouphead.next = Curr; Curr = Next;//has finished processing K nodes, the precursor of the packet head moves to the last linked node . if(Count= = k) {grouphead = Grouptail;//Counter zeroed Count=0; } }//indicates that the number of nodes is not an integral multiple of k, and will not be the node of the element at the end of the integer multiple. //Use the tail interpolation method again to restore if(Count!=0) {Curr = Grouphead.next; Grouphead.next =NULL; while(Curr! =NULL) {next = Curr.next; Curr.next = Grouphead.next; Grouphead.next = Curr; Curr = Next; } }returnRoot.next; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47034983"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "025-reverse Nodes in K-group (a set of K nodes in a single linked list to invert)"