Problem:
Reverses the one-way list with k elements as a group. Like what:
Input: 1->2->3->4->5->6->7->8->null and k = 3
Output: 3->2->1->6->5->4->8->7->null.
Analysis:
We can divide the entire list into several sub-linked lists of length k, and then we reverse each of the sub-lists (recursion). The crux of the problem is that we need to connect each sub-list together. So, for each sub-list operation, we need to return to the head of the sub-linked list, and then we set the last node of the previous sub-list, set its next to the head (head) returned by the next list, so that all the sub-linked lists are connected.
[Java] view plaincopyprint?
- Public static node reverse (node head, int k) {
- Node current = head;
- Node next = null;
- Node prev = null;
- int count = 0;
- /*reverse First k nodes of the linked list * /
- while (current! = null && count < K) {
- Next = Current.next;
- Current.next = prev;
- prev = current;
- current = next;
- count++;
- }
- /* Next is now a pointer to (k+1) th node
- Recursively the list starting from current.
- And make rest of the list as next of first node */
- if (next! = null) {
- Head.next = Reverse (next, k);
- }
- / * prev is new head of the input list * /
- return prev;
- }
public static node reverse (node head, int k) {node-current = head; Node next = null; Node prev = null; int count = 0; /*reverse first k nodes of the linked list * * while (current! = null && count < K) { Next = current . Next; Current.next = prev; prev = current; current = next; count++; } /* Next is today a pointer to (k+1) th node recursively call for the list starting from current. And make rest of the list as next of first node * /if (next! = null) { Head.next = reverse (next, k); } / * prev is new head of the input list */ return prev;}
This problem can also be used in a non-recursive way, basically the problem is handled in the same way as recursion, but the non-recursive way is slightly more complicated, because each time the list is reversed, we need to update the next value of the last node of the previous sub-list. The code is as follows:
[Java] view plaincopyprint?
- Class Node {
- int val;
- Node Next;
- Node (int x) {
- val = x;
- Next = null;
- }
- }
- Public class Solution {
- public static void Main (string[] args) {
- Solution s = new solution ();
- Node N1 = new node (1);
- Node N2 = new Node (2);
- Node N3 = new Node (3);
- Node N4 = new Node (4);
- Node N5 = new Node (5);
- N1.next = n2;
- N2.next = n3;
- N3.next = N4;
- N4.next = N5;
- Node head = s.reverseingroups (n1, 2);
- While (head! = null) {
- System.out.println (Head.val);
- head = Head.next;
- }
- }
- Public Node reverseingroups (node-current , int k) {
- if (current = = Null | | current.next = = null) return current;
- //store The new head of the list
- Node newhead = null;
- //store The last node in the sub-list,
- //we would update its next value when finishing
- //reversing the next sub-list
- Node previousgrouptail = null;
- int count = 1; //used to track the first sub-list
- while (current! = null) {
- //The actual tail in the sub-list
- Node Grouptail = current;
- //reverse
- Node prev = null;
- Node next = null;
- For (int i = 1; I <= k && current! = null; i++) {
- Next = Current.next;
- Current.next = prev;
- prev = current;
- current = next;
- }
- //Get the new head of the whole list
- if (count = = 1) {
- Newhead = prev;
- count++;
- }
- //Update the next value of the last node in
- //each sub-list
- if (previousgrouptail! = null) {
- Previousgrouptail.next = prev;
- }
- Previousgrouptail = Grouptail;
- }
- return newhead;
- }
- }
List of K-node backlinks