We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 4271502 + 000bfd8 commit 47d879dCopy full SHA for 47d879d
206. Reverse Linked List
@@ -0,0 +1,28 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode() : val(0), next(nullptr) {}
7
+ * ListNode(int x) : val(x), next(nullptr) {}
8
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
9
+ * };
10
+ */
11
+class Solution {
12
+public:
13
+ ListNode* reverseList(ListNode* head) {
14
+ if(!head)return head;
15
+ ListNode* prev=NULL;
16
+ ListNode* curr=head;
17
+ ListNode* leader;
18
+
19
+ while(curr!=NULL){
20
+ leader=curr->next;
21
+ curr->next=prev;
22
+ prev=curr;
23
+ curr=leader;
24
+ }
25
26
+ return prev;
27
28
+};
0 commit comments