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

Commite7da092

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent5903b79 commite7da092

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
structListNode {
56
intval;
67
structListNode*next;
@@ -17,15 +18,15 @@ static struct ListNode *recursive(struct ListNode *prev, struct ListNode *p)
1718
returnrecursive(p,q);
1819
}
1920

20-
staticstructListNode*reverseList(structListNode*head)
21+
structListNode*reverseList(structListNode*head)
2122
{
2223
returnrecursive(NULL,head);
2324
}
2425

2526

26-
/* Iteration */
2727
#if0
28-
staticstructListNode*reverseList(structListNode*head)
28+
/* Iteration */
29+
structListNode*reverseList(structListNode*head)
2930
{
3031
structListNode*prev=NULL;
3132
structListNode*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+
usingnamespacestd;
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+
classSolution {
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)

[8]ページ先頭

©2009-2025 Movatter.jp