From 10c569caead9222c9f758eb672ae145b7dd59abb Mon Sep 17 00:00:00 2001 From: Devansh Date: Sun, 31 Oct 2021 05:30:08 +0530 Subject: [PATCH] Reversed Linked List --- C++/Reversed _linked_list.cpp | 79 +++++++++++++++++++++++++++++++++++ contribution.md | 1 + 2 files changed, 80 insertions(+) create mode 100644 C++/Reversed _linked_list.cpp diff --git a/C++/Reversed _linked_list.cpp b/C++/Reversed _linked_list.cpp new file mode 100644 index 0000000..5236d69 --- /dev/null +++ b/C++/Reversed _linked_list.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; + +class ListNode { //class of listNode + public: + int val; //value sorted in node + ListNode *next; //pointer to next node + ListNode(int x) : val(x), next(NULL) {} //constructor +}; + +//Add new node to the end of the list and updates the end pointer +void insert_at_tail(ListNode* &end,ListNode* &head,int val){ + ListNode* n = new ListNode(val); //new node + + if(head==NULL){ //if the linked list is element , head and end pointer will points towards 1st node + head=n; + end=n; //this condition hits only one time + return; + } + end->next=n; //add pointer to the new node + end=n; //update end pointer +} + +//reverses the linked list and updates pointer accordingly +void Make_Reversed_Linked_List(ListNode* &end,ListNode* &head){ + ListNode* temp=head; //current node + ListNode* prev=NULL; //previous node + ListNode* next=NULL; //next node + ListNode* delta =head; //pointer to current head + + while(temp!=NULL){ + next=temp->next; //change pointer to previous node instead of next node + temp->next=prev; //to reverse the linked list + prev=temp; + temp=next; + } + head=prev; //update head pointer + end=delta; //update end pointer + end->next=NULL; //end node points to nothing +} + +//function to print the linked list (currently of no use but can be used to check our output) +void print(ListNode* head){ + ListNode* temp=head; + while(temp!=NULL){ //check condition if pointer to next element is null then terminates it + cout<val<<" "; //print node then move to next node + temp=temp->next; + } +} + +int main(){ + + cout<<"Enter number of nodes in Linked List"<>n; + + if( n==0){ //terminates the code if total nodes = 0 + cout<<"Size of linked list is 0, please enter size greater than 0 "; + return 0; + } + + cout<<"Enter elements of linked list"<>a; + insert_at_tail(end,head,a); //adds node to tail of linked list + } + + Make_Reversed_Linked_List(end,head); //reverses the linked list and also update head and end pointer + //accordingly + cout<<"Reversed Linked list : "; + print(head); + + + return 0; +} \ No newline at end of file diff --git a/contribution.md b/contribution.md index 180ca24..03da2cd 100644 --- a/contribution.md +++ b/contribution.md @@ -10,3 +10,4 @@ [meghala](https://github.com/m3gh4l4) [xharsshvardhan](https://github.com/xharshvardhan) [Deumus74](https://github.com/Deumus74) +[Devansh0901](https://github.com/Devansh0901)