-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day 24 reverse linkedlist iterative,recursive
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <iostream> | ||
|
||
struct Node { | ||
int data; | ||
Node * next; | ||
Node( int d ) : data{ d }, next{ nullptr } { } | ||
}; | ||
|
||
void push( Node * & head, int data ) { | ||
Node *newNode = new Node(data); | ||
if ( head == nullptr ) { | ||
head = newNode; | ||
} else { | ||
Node *curr = head; | ||
while( curr->next != nullptr ) { | ||
curr = curr->next; | ||
} | ||
curr->next = newNode; | ||
} | ||
} | ||
|
||
void printList( Node * head ) { | ||
while( head ) { | ||
std::cout << head->data << "-->"; | ||
head = head->next; | ||
} | ||
std::cout << "NULL" << std::endl; | ||
} | ||
|
||
void reverseIter( Node * & head ) { | ||
//length of list is less than 2 | ||
if ( head == nullptr || ( head != nullptr && head->next == nullptr )) { | ||
return; | ||
} | ||
Node *newHead = nullptr; | ||
Node *curr = head; | ||
Node *prev = nullptr; | ||
while( curr != nullptr ) { | ||
prev = curr; | ||
curr = curr->next; | ||
prev->next = newHead; | ||
newHead = prev; | ||
} | ||
head = newHead; | ||
} | ||
|
||
void reverseRecur( Node * & head ) { | ||
if ( head == nullptr || ( head != nullptr && head->next == nullptr ) ) { | ||
return; | ||
} | ||
Node * first = head; | ||
Node * rest = head->next; | ||
reverseIter( rest ); | ||
first->next->next = first; | ||
first->next = nullptr; | ||
head = rest; | ||
} | ||
|
||
int main() { | ||
Node *head = nullptr; | ||
push( head, 1 ); | ||
push( head, 2 ); | ||
push( head, 3 ); | ||
push( head, 4 ); | ||
push( head, 5 ); | ||
std::cout << "Before Reversing Linkedlist: "; | ||
printList( head ); | ||
reverseIter( head ); | ||
std::cout << "After Reversing Linkedlist once: "; | ||
printList( head ); | ||
reverseRecur( head ); | ||
std::cout << "After Reversing Linkedlist twice: "; | ||
printList( head ); | ||
return 0; | ||
} |