|
| 1 | +Given a singly linked list of integers, reverse the nodes of the linked list 'k' at a time and return its modified list. |
| 2 | + 'k' is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of 'k,' then left-out nodes, in the end, should be reversed as well. |
| 3 | +Example : |
| 4 | +Given this linked list: 1 -> 2 -> 3 -> 4 -> 5 |
| 5 | + |
| 6 | +For k = 2, you should return: 2 -> 1 -> 4 -> 3 -> 5 |
| 7 | + |
| 8 | +For k = 3, you should return: 3 -> 2 -> 1 -> 5 -> 4 |
| 9 | + Note : |
| 10 | +No need to print the list, it has already been taken care. Only return the new head to the list. |
| 11 | + Input format : |
| 12 | +The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. |
| 13 | + |
| 14 | +The first line of each test case or query contains the elements of the singly linked list separated by a single space. |
| 15 | + |
| 16 | +The second line of input contains a single integer depicting the value of 'k'. |
| 17 | + Remember/Consider : |
| 18 | +While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element |
| 19 | +Output format : |
| 20 | +For each test case/query, print the elements of the updated singly linked list. |
| 21 | + |
| 22 | +Output for every test case will be printed in a separate line. |
| 23 | +Constraints : |
| 24 | +1 <= t <= 10^2 |
| 25 | +0 <= M <= 10^5 |
| 26 | +Where M is the size of the singly linked list. |
| 27 | +0 <= k <= M |
| 28 | + |
| 29 | +Time Limit: 1sec |
| 30 | +Sample Input 1 : |
| 31 | +1 |
| 32 | +1 2 3 4 5 6 7 8 9 10 -1 |
| 33 | +4 |
| 34 | +Sample Output 1 : |
| 35 | +4 3 2 1 8 7 6 5 10 9 |
| 36 | +Sample Input 2 : |
| 37 | +2 |
| 38 | +1 2 3 4 5 -1 |
| 39 | +0 |
| 40 | +10 20 30 40 -1 |
| 41 | +4 |
| 42 | +Sample Output 2 : |
| 43 | +1 2 3 4 5 |
| 44 | +40 30 20 10 |
| 45 | + |
| 46 | +*****************************************Code*************************************** |
| 47 | +public class Solution { |
| 48 | + |
| 49 | + public static LinkedListNode<Integer> kReverse(LinkedListNode<Integer> head, int k) { |
| 50 | + |
| 51 | + if(head==null || head.next==null) |
| 52 | + return head; |
| 53 | + if(k==0) |
| 54 | + return head; |
| 55 | + LinkedListNode<Integer> temp=head,h1=head,h2; |
| 56 | + int cnt=1; |
| 57 | + while(cnt<k && temp.next!=null) |
| 58 | + { |
| 59 | + temp=temp.next; |
| 60 | + cnt++; |
| 61 | + } |
| 62 | + h2=temp.next; |
| 63 | + temp.next=null; |
| 64 | + DoubleNode ans=reverse(h1); |
| 65 | + LinkedListNode<Integer> head2=kReverse(h2,k); |
| 66 | + ans.tail.next=head2; |
| 67 | + return ans.head; |
| 68 | + } |
| 69 | + private static DoubleNode reverse(LinkedListNode<Integer> head) |
| 70 | + { |
| 71 | + if(head==null || head.next==null) |
| 72 | + { |
| 73 | + DoubleNode ans=new DoubleNode(); |
| 74 | + ans.head=head; |
| 75 | + ans.tail=head; |
| 76 | + return ans; |
| 77 | + } |
| 78 | + DoubleNode ans=reverse(head.next); |
| 79 | + ans.tail.next=head; |
| 80 | + head.next=null; |
| 81 | + ans.tail=ans.tail.next; |
| 82 | + return ans; |
| 83 | + } |
| 84 | +} |
| 85 | + class DoubleNode |
| 86 | + { |
| 87 | + LinkedListNode<Integer> head; |
| 88 | + LinkedListNode<Integer> tail; |
| 89 | + } |
| 90 | + |
0 commit comments