|  | 
|  | 1 | +/* | 
|  | 2 | + | 
|  | 3 | +Author : Saransh Bangar | 
|  | 4 | +Date : 21/03/2024 | 
|  | 5 | +Problem : Reverse Linked List | 
|  | 6 | +Difficulty : Easy  | 
|  | 7 | +Problem Link : https://leetcode.com/problems/reverse-linked-list/description/ | 
|  | 8 | +Video Solution : NA | 
|  | 9 | + | 
|  | 10 | +*/ | 
|  | 11 | + | 
|  | 12 | + | 
|  | 13 | +/** | 
|  | 14 | + * Definition for singly-linked list. | 
|  | 15 | + * struct ListNode { | 
|  | 16 | + * int val; | 
|  | 17 | + * ListNode *next; | 
|  | 18 | + * ListNode() : val(0), next(nullptr) {} | 
|  | 19 | + * ListNode(int x) : val(x), next(nullptr) {} | 
|  | 20 | + * ListNode(int x, ListNode *next) : val(x), next(next) {} | 
|  | 21 | + * }; | 
|  | 22 | + */ | 
|  | 23 | +class Solution { | 
|  | 24 | +public: | 
|  | 25 | + ListNode* reverseList(ListNode* head) | 
|  | 26 | + { | 
|  | 27 | + vector<int>help; | 
|  | 28 | + ListNode* temp=head; | 
|  | 29 | + while (temp!=nullptr) | 
|  | 30 | + { | 
|  | 31 | + help.push_back(temp->val); | 
|  | 32 | + temp=temp->next; | 
|  | 33 | + } | 
|  | 34 | + temp=head; | 
|  | 35 | + for (int i=help.size()-1;i>=0;i--) | 
|  | 36 | + { | 
|  | 37 | + temp->val=help[i]; | 
|  | 38 | + temp=temp->next; | 
|  | 39 | + } | 
|  | 40 | + return head; | 
|  | 41 | + } | 
|  | 42 | +}; | 
0 commit comments