`{"id":"8657158e-7ecd-447e-8922-c592b0f547d3","name":"Reverse Node Of Linkedlist In K Group","description":"1. Given a singly linklist, reverse the nodes of a linked list k at a time and return its modified linkedlist.\r\n2. If number of nodes in multiple of k then it will reverse otherwise it will add inn the end of linkedlist without any change.\r\n","inputFormat":"1->5->2->9->5->14->11->1->10->10->1->3->null\r\n7","outputFormat":"11->14->5->9->2->5->1->10->10->1->3->null","constraints":"1 &lt;= size Of LinkedList &lt;= 10^6\r\n0 &lt;= k &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode* next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode* reverseInKGroup(ListNode* head, int k)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode* node)\r\n{\r\n ListNode* curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode* createList(int n)\r\n{\r\n ListNode* dummy = new ListNode(-1);\r\n ListNode* prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode* h1 = createList(n);\r\n int k;\r\n cin >> k;\r\n h1 = reverseInKGroup(h1, k);\r\n printList(h1);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode reverseInKGroup(ListNode head, int k) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n int k = scn.nextInt();\r\n h1 = reverseInKGroup(h1, k);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"12\r\n1 5 2 9 5 14 11 1 10 10 1 3 \r\n7\r\n","sampleOutput":"11 14 5 9 2 5 1 1 10 10 1 3 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"4d5549bd-ad7e-4a93-abe2-0a6c0aedb0ab","name":"Reverse Node Of Linkedlist In K Group","slug":"reverse-node-of-linkedlist-in-k-group","type":1}],"next":{"id":"1c5faa56-6ff3-490f-8631-77b6700b90c1","name":"Reverse Node Of LinkedList In K Group MCQ","type":0,"slug":"reverse-node-of-linkedlist-in-k-group-mcq"},"prev":{"id":"fa0fabeb-6032-4f1a-8f3b-826b1bff07c1","name":"Segregate Even And Odd Nodes In A LinkedList","type":3,"slug":"segregate-even-and-odd-nodes-in-a-linkedlist"}}}`

# Reverse Node Of Linkedlist In K Group

1. Given a singly linklist, reverse the nodes of a linked list k at a time and return its modified linkedlist. 2. If number of nodes in multiple of k then it will reverse otherwise it will add inn the end of linkedlist without any change.

`{"id":"8657158e-7ecd-447e-8922-c592b0f547d3","name":"Reverse Node Of Linkedlist In K Group","description":"1. Given a singly linklist, reverse the nodes of a linked list k at a time and return its modified linkedlist.\r\n2. If number of nodes in multiple of k then it will reverse otherwise it will add inn the end of linkedlist without any change.\r\n","inputFormat":"1->5->2->9->5->14->11->1->10->10->1->3->null\r\n7","outputFormat":"11->14->5->9->2->5->1->10->10->1->3->null","constraints":"1 &lt;= size Of LinkedList &lt;= 10^6\r\n0 &lt;= k &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode* next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode* reverseInKGroup(ListNode* head, int k)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode* node)\r\n{\r\n ListNode* curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode* createList(int n)\r\n{\r\n ListNode* dummy = new ListNode(-1);\r\n ListNode* prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode* h1 = createList(n);\r\n int k;\r\n cin >> k;\r\n h1 = reverseInKGroup(h1, k);\r\n printList(h1);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode reverseInKGroup(ListNode head, int k) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n int k = scn.nextInt();\r\n h1 = reverseInKGroup(h1, k);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"12\r\n1 5 2 9 5 14 11 1 10 10 1 3 \r\n7\r\n","sampleOutput":"11 14 5 9 2 5 1 1 10 10 1 3 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"4d5549bd-ad7e-4a93-abe2-0a6c0aedb0ab","name":"Reverse Node Of Linkedlist In K Group","slug":"reverse-node-of-linkedlist-in-k-group","type":1}],"next":{"id":"1c5faa56-6ff3-490f-8631-77b6700b90c1","name":"Reverse Node Of LinkedList In K Group MCQ","type":0,"slug":"reverse-node-of-linkedlist-in-k-group-mcq"},"prev":{"id":"fa0fabeb-6032-4f1a-8f3b-826b1bff07c1","name":"Segregate Even And Odd Nodes In A LinkedList","type":3,"slug":"segregate-even-and-odd-nodes-in-a-linkedlist"}}}`

Editor

# Reverse Node Of Linkedlist In K Group

easy

1. Given a singly linklist, reverse the nodes of a linked list k at a time and return its modified linkedlist. 2. If number of nodes in multiple of k then it will reverse otherwise it will add inn the end of linkedlist without any change.

## Constraints

1 <= size Of LinkedList <= 10^6 0 <= k <= 10^6

## Format

### Input

1->5->2->9->5->14->11->1->10->10->1->3->null 7

### Output

11->14->5->9->2->5->1->10->10->1->3->null

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}12 1 5 2 9 5 14 11 1 10 10 1 3 7 ```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}11 14 5 9 2 5 1 1 10 10 1 3 `

Discussions

Show Discussion

Related Resources