- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reorder List in C++
Suppose we have a linked list like l1 -> l2 -> l3 -> l4 -> … -> l(n-1) -> ln. We have to rearrange this list into the form l1 -> ln -> l2 -> l(n - 1) -> … and so on. Here the constraint is that, we cannot modify the values in the list nodes, only the node itself may be changed. So for example, if the list is like [1,2,3,4,5], then the output will be [1,5,2,4,3]
To solve this, we will follow these steps −
Define a method called reverse to perform the reverse operation. This will take node head and node prev. This will act like below −
if head is null, then return prev
temp := next of head
next of head := prev, and prev := head
return reverse(temp, prev)
The reordering task will be like below −
if head is null, then return null
define two node pointers called slow and fast, and initialize them with head
while fast and next of fast both are not null,
slow := next of slow
fast := next of next of fast
fast := reverse(next of slow)
set next of slow as null, and slow := head
define two list node pointers temp1 and temp2
while fast is not null
temp1 := next of slow, temp2 := next of fast
next of slow := fast, next of fast := temp1
slow := temp1, fast := temp2
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class ListNode{ public: int val; ListNode *next; ListNode(int data){ val = data; next = NULL; } }; ListNode *make_list(vector<int> v){ ListNode *head = new ListNode(v[0]); for(int i = 1; i<v.size(); i++){ ListNode *ptr = head; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = new ListNode(v[i]); } return head; } void print_list(ListNode *head){ ListNode *ptr = head; cout << "["; while(ptr){ cout << ptr->val << ", "; ptr = ptr->next; } cout << "]" << endl; } class Solution { public: ListNode* successor = NULL; ListNode* reverse(ListNode* head, ListNode* prev = NULL){ if(!head)return prev; ListNode* temp = head->next; head->next = prev; prev = head; return reverse(temp, prev); } void reorderList(ListNode* head) { if(!head)return; ListNode* slow = head; ListNode* fast = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; } fast = reverse(slow->next); slow->next = NULL; slow = head; ListNode *temp1, *temp2; while(fast){ temp1 = slow->next; temp2 = fast->next; slow->next = fast; fast->next = temp1; slow = temp1; fast = temp2; } } }; main(){ vector<int> v = {1,2,3,4,5}; ListNode *h1 = make_list(v); Solution ob; (ob.reorderList(h1)); print_list(h1); }
Input
[1,2,3,4,5]
Output
[1, 5, 2, 4, 3, ]