Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit355226b

Browse files
authored
Merge pull requestneetcode-gh#2448 from KhizarShabir1/update_solution_of_reverse-linked-list
simplified code to only use two named pointers in reverese linked list (C++)
2 parentsfc3582a +b9c3419 commit355226b

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

‎cpp/0206-reverse-linked-list.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Given the head of a singly linked list, reverse list & return
33
Ex. head = [1,2,3,4,5] -> [5,4,3,2,1], head = [1,2] -> [2,1]
44
5-
Maintain prev, curr, & next pointers, iterate thru & reverse
5+
Maintain prev, curr pointers, iterate thru & reverse
66
77
Time: O(n)
88
Space: O(1)
@@ -18,24 +18,24 @@
1818
* ListNode(int x, ListNode *next) : val(x), next(next) {}
1919
* };
2020
*/
21-
classSolution {
21+
classSolution
22+
{
2223
public:
23-
ListNode*reverseList(ListNode* head) {
24-
if (head ==NULL || head->next ==NULL) {
24+
ListNode *reverseList(ListNode *head)
25+
{
26+
if (head ==NULL || head->next ==NULL)
2527
return head;
26-
}
27-
28-
ListNode* prev =NULL;
29-
ListNode* curr = head;
30-
ListNode* next = curr->next;
31-
32-
while (curr !=NULL) {
33-
next = curr->next;
28+
29+
ListNode *prev =NULL;
30+
ListNode *curr = head;
31+
32+
while (curr !=NULL)
33+
{
34+
ListNode *temp = curr->next;
3435
curr->next = prev;
3536
prev = curr;
36-
curr =next;
37+
curr =temp;
3738
}
38-
3939
return prev;
4040
}
4141
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp