From e3a56b7b0003deb682025eee6113377d9a7c3e4d Mon Sep 17 00:00:00 2001 From: Bhumika Tewary <77784592+bhumikatewary@users.noreply.github.com> Date: Tue, 5 Apr 2022 12:01:54 +0530 Subject: [PATCH] Create Reverse LinkedList file --- .../linkedlist/ReverseLinkedList.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 algorithms/data-structures/linkedlist/ReverseLinkedList.cpp diff --git a/algorithms/data-structures/linkedlist/ReverseLinkedList.cpp b/algorithms/data-structures/linkedlist/ReverseLinkedList.cpp new file mode 100644 index 00000000..cdaf7f56 --- /dev/null +++ b/algorithms/data-structures/linkedlist/ReverseLinkedList.cpp @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + ListNode* curr=head; + ListNode* prev=NULL; + + while(curr!=NULL){ + ListNode* next=curr->next; + curr->next=prev; + prev=curr; + curr=next; + } + return prev; + } +}; + +//Contributed by bhumikatewary