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

Commit e7da092

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 5903b79 commit e7da092

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

‎0206_reverse_linked_list/reverse_list.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
45
struct ListNode {
56
int val;
67
struct ListNode *next;
@@ -17,15 +18,15 @@ static struct ListNode *recursive(struct ListNode *prev, struct ListNode *p)
1718
return recursive(p, q);
1819
}
1920

20-
staticstruct ListNode *reverseList(struct ListNode *head)
21+
struct ListNode *reverseList(struct ListNode *head)
2122
{
2223
return recursive(NULL, head);
2324
}
2425

2526

26-
/* Iteration */
2727
#if 0
28-
static struct ListNode *reverseList(struct ListNode *head)
28+
/* Iteration */
29+
struct ListNode *reverseList(struct ListNode *head)
2930
{
3031
struct ListNode *prev = NULL;
3132
struct ListNode *p = head;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* struct ListNode {
8+
* int val;
9+
* ListNode *next;
10+
* ListNode() : val(0), next(nullptr) {}
11+
* ListNode(int x) : val(x), next(nullptr) {}
12+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
13+
* };
14+
*/
15+
class Solution {
16+
public:
17+
ListNode* reverseList(ListNode* head) {
18+
ListNode *prev = nullptr;
19+
ListNode *p = head;
20+
while (p != nullptr) {
21+
ListNode *q = p->next;
22+
p->next = prev;
23+
prev = p;
24+
p = q;
25+
}
26+
return prev;
27+
}
28+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /