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