From aefab2dd49f166a36c1d17966ab966f340a5152a Mon Sep 17 00:00:00 2001 From: Jatin kansal <43703274+jatinkansal2005@users.noreply.github.com> Date: Thu, 20 Oct 2022 01:05:28 +0530 Subject: [PATCH] Create reverse_linked_list.cpp --- C++/reverse_linked_list.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 C++/reverse_linked_list.cpp diff --git a/C++/reverse_linked_list.cpp b/C++/reverse_linked_list.cpp new file mode 100644 index 0000000..734cd46 --- /dev/null +++ b/C++/reverse_linked_list.cpp @@ -0,0 +1,29 @@ +/** + * 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; + if(head==NULL) + return NULL; + while(curr->next!=NULL) + { + ListNode* curr1=curr->next; + curr->next=prev; + prev=curr; + curr=curr1; + } + head=curr; + curr->next=prev; + return head; + } +};