`{"id":"920fe2fc-e459-4334-9ae8-c76442874857","name":"Fold Of Linkedlist","description":"Given a singly linkedlist : l0 -> l1 -> l2 -> l3 -> l4 -> l5 -> l6 ..... -> ln-1 -> ln \r\nreorder it : l0 -> ln -> l1 -> ln-1 -> l2 -> ln-2 -> l3 -> ln-3 -> ..... \r\n\r\nfor more information watch video.\r\n","inputFormat":"1->2->3->4->5->6->7->null\r\n","outputFormat":"1->7->2->6->3->5->4->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 fold(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 fold(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 fold(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 fold(head);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"10\r\n5\r\n1\r\n4\r\n6\r\n9\r\n9\r\n6\r\n4\r\n1\r\n5\r\n","sampleOutput":"5 5 1 1 4 4 6 6 9 9 ","questionVideo":"https://www.youtube.com/embed/fNhdeMLah0I","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":"7401aa82-08b6-443f-a628-35bb6950bdfd","name":"Fold Of Linkedlist","slug":"fold-of-linkedlist","type":1}],"next":{"id":"21c1e422-b430-4cee-962a-e09ef7c77a4f","name":"Fold of Linkedlist","type":3,"slug":"fold-of-linkedlist-6495"},"prev":{"id":"2cafad86-44f1-4860-9a9a-cdbd7c49a587","name":"Palindrome LinkedList","type":3,"slug":"palindrome-linkedlist"}}}`

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

`{"id":"920fe2fc-e459-4334-9ae8-c76442874857","name":"Fold Of Linkedlist","description":"Given a singly linkedlist : l0 -> l1 -> l2 -> l3 -> l4 -> l5 -> l6 ..... -> ln-1 -> ln \r\nreorder it : l0 -> ln -> l1 -> ln-1 -> l2 -> ln-2 -> l3 -> ln-3 -> ..... \r\n\r\nfor more information watch video.\r\n","inputFormat":"1->2->3->4->5->6->7->null\r\n","outputFormat":"1->7->2->6->3->5->4->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 fold(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 fold(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 fold(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 fold(head);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"10\r\n5\r\n1\r\n4\r\n6\r\n9\r\n9\r\n6\r\n4\r\n1\r\n5\r\n","sampleOutput":"5 5 1 1 4 4 6 6 9 9 ","questionVideo":"https://www.youtube.com/embed/fNhdeMLah0I","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":"7401aa82-08b6-443f-a628-35bb6950bdfd","name":"Fold Of Linkedlist","slug":"fold-of-linkedlist","type":1}],"next":{"id":"21c1e422-b430-4cee-962a-e09ef7c77a4f","name":"Fold of Linkedlist","type":3,"slug":"fold-of-linkedlist-6495"},"prev":{"id":"2cafad86-44f1-4860-9a9a-cdbd7c49a587","name":"Palindrome LinkedList","type":3,"slug":"palindrome-linkedlist"}}}`

Editor

easy

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

0 <= N <= 10^6

## Format

### Input

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

### Output

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

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

Question Video

Discussions

Show Discussion

Related Resources