diff --git a/linked_list_problems/Reverse_linked_list.cpp b/linked_list_problems/Reverse_linked_list.cpp new file mode 100644 index 0000000..0ff73cf --- /dev/null +++ b/linked_list_problems/Reverse_linked_list.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* prev = NULL; + ListNode* nxt = NULL; + ListNode* temp = head; + + while(temp != NULL) + { + nxt = temp->next; + temp->next = prev; + prev = temp; + temp = nxt; + } + return prev; + } +};