List of K-node backlinks

Source: Internet
Author: User

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?
  1. Public static node reverse (node head, int k) {
  2. Node current = head;
  3. Node next = null;
  4. Node prev = null;
  5. int count = 0;
  6. /*reverse First k nodes of the linked list * /
  7. while (current! = null && count < K) {
  8. Next = Current.next;
  9. Current.next = prev;
  10. prev = current;
  11. current = next;
  12. count++;
  13. }
  14. /* Next is now a pointer to (k+1) th node
  15. Recursively the list starting from current.
  16. And make rest of the list as next of first node */
  17. if (next! = null) {
  18. Head.next = Reverse (next, k);
  19. }
  20. / * prev is new head of the input list * /
  21. return prev;
  22. }
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?
  1. Class Node {
  2. int val;
  3. Node Next;
  4. Node (int x) {
  5. val = x;
  6. Next = null;
  7. }
  8. }
  9. Public class Solution {
  10. public static void Main (string[] args) {
  11. Solution s = new solution ();
  12. Node N1 = new node (1);
  13. Node N2 = new Node (2);
  14. Node N3 = new Node (3);
  15. Node N4 = new Node (4);
  16. Node N5 = new Node (5);
  17. N1.next = n2;
  18. N2.next = n3;
  19. N3.next = N4;
  20. N4.next = N5;
  21. Node head = s.reverseingroups (n1, 2);
  22. While (head! = null) {
  23. System.out.println (Head.val);
  24. head = Head.next;
  25. }
  26. }
  27. Public Node reverseingroups (node-current , int k) {
  28. if (current = = Null | | current.next = = null) return current;
  29. //store The new head of the list
  30. Node newhead = null;
  31. //store The last node in the sub-list,
  32. //we would update its next value when finishing
  33. //reversing the next sub-list
  34. Node previousgrouptail = null;
  35. int count = 1; //used to track the first sub-list
  36. while (current! = null) {
  37. //The actual tail in the sub-list
  38. Node Grouptail = current;
  39. //reverse
  40. Node prev = null;
  41. Node next = null;
  42. For (int i = 1; I <= k && current! = null; i++) {
  43. Next = Current.next;
  44. Current.next = prev;
  45. prev = current;
  46. current = next;
  47. }
  48. //Get the new head of the whole list
  49. if (count = = 1) {
  50. Newhead = prev;
  51. count++;
  52. }
  53. //Update the next value of the last node in
  54. //each sub-list
  55. if (previousgrouptail! = null) {
  56. Previousgrouptail.next = prev;
  57. }
  58. Previousgrouptail = Grouptail;
  59. }
  60. return newhead;
  61. }
  62. }

List of K-node backlinks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.