How to reverse a doubly linked list ?

I talked about how to reverse a singly linked list earlier. That was slightly tricky to understand. Reversing a doubly linked list is relatively easy. The logic is : You need to keep on changing the next and previous pointers as you traverse the entire list. Here is the code snippet in Java : **public** **void** **reverse**() { **if** (first == **null**) **return**; DoubleNode previous = first; DoubleNode current = first. [Read More]

Write a C Program to reverse a stack "in place" using recursion ?

**You can only use the following ADT functions on Stack: IsEmpty IsFull Push Pop Top** #include #include using namespace std; stack S; void func(int n){ if(S.empty()) S.push(n); else{ int t= S.top(); S.pop(); func(n); S.push(t); } } void Rec(){ if(!S.empty()){ int t = S.top(); S.pop(); Rec(); func(t); } } void printStack(){ if(!S.empty()){ int t= S.top(); S.pop(); cout«”,"; printStack(); S.push(t); } else{ cout«"Stack empty now”; } } int main(int argc, char* argv[]) [Read More]

Reverse a linked list

Problem Reverse a linked list. Follow up - Can you do it with only 2 pointers. Solution This is one of the famous interview questions. Following are two procedures to reverse a single linked list: Iterative Procedure Recursive Procedure Iterative solution Method 1 - Iterative Procedure The following are the sequence of steps to be followed: Initially take three pointers: PrevNode, CurrNode, NextNode Let CurrNode point to HeaderNode of the list. [Read More]