{"id":"66726937-be28-4278-99bd-990ba1bc11b0","name":"Reverse In Range","description":"1. Given a singly linklist, Reverse a linkedlist from position starting position to end position.\r\n2. Do it in one-pass. without using any extra space.\r\n3. Indexing start from numeric 1.\r\n","inputFormat":"8->8->14->1->10->12->null \r\n3\r\n5","outputFormat":"8->8->10->1->14->12->null","constraints":"1 &lt;= size Of LinkedList &lt;= 10^6\r\n1 &lt;= starting Position, ending Position &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 *reverseInRange(ListNode *head, int m, int n)\r\n{\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 m, n;\r\n cin >> m >> n;\r\n h1 = reverseInRange(h1, m, n);\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 reverseInRange(ListNode head, int n, int m) {\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 sz = scn.nextInt();\r\n ListNode h1 = createList(sz);\r\n\r\n int m = scn.nextInt();\r\n int n = scn.nextInt();\r\n\r\n h1 = reverseInRange(h1, m, n);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n8 8 14 1 10 12 \r\n3\r\n5\r\n","sampleOutput":"8 8 10 1 14 12 ","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":"920cd0c1-1090-4b09-8463-e76e414b281c","name":"Reverse In Range","slug":"reverse-in-range","type":1}],"next":{"id":"c0cb1b8c-b117-42a0-ab54-45ce3bbc0334","name":"Reverse In Range","type":3,"slug":"reverse-in-range"},"prev":{"id":"e36c3b59-6e96-49c1-9c37-489662824544","name":"Reverse Node Of Linkedlist In K Group","type":3,"slug":"reverse-node-of-linkedlist-in-k-group"}}}

Reverse In Range

1. Given a singly linklist, Reverse a linkedlist from position starting position to end position. 2. Do it in one-pass. without using any extra space. 3. Indexing start from numeric 1.

{"id":"66726937-be28-4278-99bd-990ba1bc11b0","name":"Reverse In Range","description":"1. Given a singly linklist, Reverse a linkedlist from position starting position to end position.\r\n2. Do it in one-pass. without using any extra space.\r\n3. Indexing start from numeric 1.\r\n","inputFormat":"8->8->14->1->10->12->null \r\n3\r\n5","outputFormat":"8->8->10->1->14->12->null","constraints":"1 &lt;= size Of LinkedList &lt;= 10^6\r\n1 &lt;= starting Position, ending Position &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 *reverseInRange(ListNode *head, int m, int n)\r\n{\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 m, n;\r\n cin >> m >> n;\r\n h1 = reverseInRange(h1, m, n);\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 reverseInRange(ListNode head, int n, int m) {\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 sz = scn.nextInt();\r\n ListNode h1 = createList(sz);\r\n\r\n int m = scn.nextInt();\r\n int n = scn.nextInt();\r\n\r\n h1 = reverseInRange(h1, m, n);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n8 8 14 1 10 12 \r\n3\r\n5\r\n","sampleOutput":"8 8 10 1 14 12 ","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":"920cd0c1-1090-4b09-8463-e76e414b281c","name":"Reverse In Range","slug":"reverse-in-range","type":1}],"next":{"id":"c0cb1b8c-b117-42a0-ab54-45ce3bbc0334","name":"Reverse In Range","type":3,"slug":"reverse-in-range"},"prev":{"id":"e36c3b59-6e96-49c1-9c37-489662824544","name":"Reverse Node Of Linkedlist In K Group","type":3,"slug":"reverse-node-of-linkedlist-in-k-group"}}}
plane

Editor


Loading...

Reverse In Range

easy

1. Given a singly linklist, Reverse a linkedlist from position starting position to end position. 2. Do it in one-pass. without using any extra space. 3. Indexing start from numeric 1.

Constraints

1 <= size Of LinkedList <= 10^6 1 <= starting Position, ending Position <= 10^6

Format

Input

8->8->14->1->10->12->null 3 5

Output

8->8->10->1->14->12->null

Example

Sample Input

6 8 8 14 1 10 12 3 5

Sample Output

8 8 10 1 14 12

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode