diff --git a/linked-lists/README.md b/linked-lists/README.md index 4b76e1bd..311ed256 100644 --- a/linked-lists/README.md +++ b/linked-lists/README.md @@ -3,6 +3,7 @@ ### C or C++ 1. [Singly Linked List](c-or-cpp/singly.cpp) +2. [Reversing Linked List](c-or-cpp/reverse-linkedlist.cpp) ### Java diff --git a/linked-lists/c-or-cpp/reverse-linkedlist.cpp b/linked-lists/c-or-cpp/reverse-linkedlist.cpp new file mode 100644 index 00000000..3658e9de --- /dev/null +++ b/linked-lists/c-or-cpp/reverse-linkedlist.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +struct Node +{ + int data; + Node *next; +}; + +Node *newNode(int data) +{ + Node *new_node = new Node; + new_node->data = data; + new_node->next = NULL; + return new_node; +} + +void push(Node **head_ref, int new_data) +{ + Node *new_node = new Node(); + new_node->data = new_data; + new_node->next = (*head_ref); + (*head_ref) = new_node; +} + +Node *reverseList(Node *A) +{ + Node *current, *prev, *next; + current = A; + prev = NULL; + next = NULL; + while (current != NULL) + { + + // storing address of node next to current + next = current->next; + + // reversing the link of current node + current->next = prev; + + // updating prev and current + prev = current; + current = next; + } + + current = prev; + + return current; +} + +void printlist(Node *head) +{ + while (head != NULL) + { + cout << head->data << " "; + head = head->next; + } + cout << endl; +} + +int32_t main() +{ + Node *head = NULL; + push(&head, 5); + push(&head, 7); + push(&head, 10); + + cout << "Linked list before reversing ->" << endl; + printlist(head); + + head = reverseList(head); + + cout << "Linked list after reversing ->" << endl; + printlist(head); +} \ No newline at end of file