`{"id":"3b4a2cb1-2c57-4a62-ac59-7428d7c8a565","name":"Unfold Of Linkedlist","description":"Given a singly linkedlist : l0 -> ln -> l1 -> ln-1 -> l2 -> ln-2 -> l3 -> ln-3 -> ..... \r\nreorder it : l0 -> l1 -> l2 -> l3 -> l4 -> l5 -> l6 ..... -> ln-1 -> ln\r\nfor more information watch video.","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"1->2->3->4->5->6->7->null\r\n","constraints":"0 &lt;= N &lt;= 10^6","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\nvoid unfold(ListNode *head)\r\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\nint main()\r\n{\r\n int n;\r\n cin >> 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\r\n ListNode *head = dummy->next;\r\n unfold(head);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\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 void unfold(ListNode head) {\r\n\r\n }\r\n\r\n 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 void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\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 ListNode head = dummy.next;\r\n unfold(head);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"9 5 1 1 4 4 6 6 9 9 ","sampleOutput":"5 1 4 6 9 9 6 4 1 ","questionVideo":"https://www.youtube.com/embed/GjhGhEoZ1Vk","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":"04a0f4ca-5264-4d4b-a6f6-513d49ff52ee","name":"Unfold Of Linkedlist","slug":"unfold-of-linkedlist","type":1}],"next":{"id":"050d77c9-5dcc-4407-8caa-6c913f458b1e","name":"Unfold Of Linkedlist MCQ","type":0,"slug":"unfold-of-linkedlist-mcq"},"prev":{"id":"c9224b6e-2ec6-4827-b19f-5a75fe5dd894","name":"Fold Of Linkedlist MCQ","type":0,"slug":"fold-of-linkedlist-mcq"}}}`

Given a singly linkedlist : l0 -> ln -> l1 -> ln-1 -> l2 -> ln-2 -> l3 -> ln-3 -> ..... reorder it : l0 -> l1 -> l2 -> l3 -> l4 -> l5 -> l6 ..... -> ln-1 -> ln for more information watch video.

`{"id":"3b4a2cb1-2c57-4a62-ac59-7428d7c8a565","name":"Unfold Of Linkedlist","description":"Given a singly linkedlist : l0 -> ln -> l1 -> ln-1 -> l2 -> ln-2 -> l3 -> ln-3 -> ..... \r\nreorder it : l0 -> l1 -> l2 -> l3 -> l4 -> l5 -> l6 ..... -> ln-1 -> ln\r\nfor more information watch video.","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"1->2->3->4->5->6->7->null\r\n","constraints":"0 &lt;= N &lt;= 10^6","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\nvoid unfold(ListNode *head)\r\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\nint main()\r\n{\r\n int n;\r\n cin >> 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\r\n ListNode *head = dummy->next;\r\n unfold(head);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\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 void unfold(ListNode head) {\r\n\r\n }\r\n\r\n 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 void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\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 ListNode head = dummy.next;\r\n unfold(head);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"9 5 1 1 4 4 6 6 9 9 ","sampleOutput":"5 1 4 6 9 9 6 4 1 ","questionVideo":"https://www.youtube.com/embed/GjhGhEoZ1Vk","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":"04a0f4ca-5264-4d4b-a6f6-513d49ff52ee","name":"Unfold Of Linkedlist","slug":"unfold-of-linkedlist","type":1}],"next":{"id":"050d77c9-5dcc-4407-8caa-6c913f458b1e","name":"Unfold Of Linkedlist MCQ","type":0,"slug":"unfold-of-linkedlist-mcq"},"prev":{"id":"c9224b6e-2ec6-4827-b19f-5a75fe5dd894","name":"Fold Of Linkedlist MCQ","type":0,"slug":"fold-of-linkedlist-mcq"}}}`

Editor

easy

Given a singly linkedlist : l0 -> ln -> l1 -> ln-1 -> l2 -> ln-2 -> l3 -> ln-3 -> ..... reorder it : l0 -> l1 -> l2 -> l3 -> l4 -> l5 -> l6 ..... -> ln-1 -> ln for more information watch video.

0 <= N <= 10^6

## Format

### Input

1->7->2->6->3->5->4->null

### Output

1->2->3->4->5->6->7->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;}9 5 1 1 4 4 6 6 9 9 `

### 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;}5 1 4 6 9 9 6 4 1 `

Question Video

Discussions

Show Discussion

Related Resources