Advertisement
1988coder

25. Reverse Nodes in k-Group

Oct 6th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/reverse-nodes-in-k-group/
  2.  
  3. //   Definition for singly-linked list.
  4. // class ListNode {
  5. //     int val;
  6. //     ListNode next;
  7.  
  8. //     ListNode(int x) {
  9. //         val = x;
  10. //     }
  11. // }
  12.  
  13. /**
  14.  * Iterative Solution
  15.  *
  16.  * Time Complexity: O(N). Each node is visited twice.
  17.  *
  18.  * Space Complexity: O(1)
  19.  */
  20. class Solution1 {
  21.     public ListNode reverseKGroup(ListNode head, int k) {
  22.         if (head == null || head.next == null || k <= 1) {
  23.             return head;
  24.         }
  25.  
  26.         ListNode dummyHead = new ListNode(-1);
  27.         dummyHead.next = head;
  28.         ListNode leftGrpTail = dummyHead;
  29.  
  30.         while (true) {
  31.             ListNode grpHead = leftGrpTail.next;
  32.             ListNode current = grpHead;
  33.             ListNode previous = null;
  34.             int count = 0;
  35.             while (current != null && count < k) {
  36.                 previous = current;
  37.                 current = current.next;
  38.                 count++;
  39.             }
  40.             if (count < k) {
  41.                 break;
  42.             }
  43.  
  44.             ListNode rightGrpHead = current;
  45.  
  46.             // Sperating the current group from right group.
  47.             previous.next = null;
  48.  
  49.             // Rotate the current group
  50.             current = grpHead;
  51.             previous = null;
  52.             while (current != null) {
  53.                 ListNode next = current.next;
  54.                 current.next = previous;
  55.                 previous = current;
  56.                 current = next;
  57.             }
  58.  
  59.             // After rotating current group
  60.             // previous points to head of current group.
  61.             // grpHead variable points to tail of the current group.
  62.             leftGrpTail.next = previous;
  63.             grpHead.next = rightGrpHead;
  64.             leftGrpTail = grpHead;
  65.         }
  66.  
  67.         return dummyHead.next;
  68.     }
  69. }
  70.  
  71. /**
  72.  * Recursive Solution
  73.  *
  74.  * Time Complexity: O(N)
  75.  *
  76.  * Space Complexity: O(N/k)
  77.  */
  78. class Solution2 {
  79.     public ListNode reverseKGroup(ListNode head, int k) {
  80.         if (head == null || head.next == null || k <= 1) {
  81.             return head;
  82.         }
  83.  
  84.         ListNode current = head;
  85.         ListNode previous = null;
  86.         int count = 0;
  87.         while (current != null && count < k) {
  88.             previous = current;
  89.             current = current.next;
  90.             count++;
  91.         }
  92.  
  93.         if (count == k) {
  94.             previous.next = null;
  95.             ListNode rightSideHead = reverseKGroup(current, k);
  96.  
  97.             current = head;
  98.             previous = null;
  99.             while (current != null) {
  100.                 ListNode next = current.next;
  101.                 current.next = previous;
  102.                 previous = current;
  103.                 current = next;
  104.             }
  105.             head.next = rightSideHead;
  106.             head = previous;
  107.         }
  108.  
  109.         return head;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement