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 8649a12

Browse files
add cpp solution
1 parent 8a98916 commit 8649a12

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* reverseBetween(ListNode* head, int m, int n) {
12+
ListNode headNode(0) ;
13+
headNode.next = head ;
14+
15+
n = n-m+1 ;
16+
17+
ListNode *p = &headNode ;
18+
while (--m) // 移动m-1次,故--m
19+
p = p->next ;
20+
21+
ListNode revNode(0) ;
22+
ListNode *pRev = &revNode ;
23+
pRev->next = nullptr ;
24+
ListNode *pN = p->next ;
25+
26+
ListNode *pTmp ;
27+
while (n--)
28+
{
29+
pTmp = p->next ;
30+
p->next = p->next->next ;
31+
pTmp->next = pRev->next ;
32+
pRev->next = pTmp ;
33+
}
34+
35+
pN->next = p->next ;
36+
p->next = revNode.next ;
37+
38+
return headNode.next ;
39+
}
40+
};

0 commit comments

Comments
(0)

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