Skip to content

Commit a327e08

Browse files
Time: 0 ms (100.00%), Space: 7.4 MB (59.91%) - LeetHub
1 parent 61d9d2f commit a327e08

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
14+
15+
ListNode* reverseBetween(ListNode* head, int left, int right) {
16+
if(left==right)
17+
{
18+
return head;
19+
}
20+
21+
22+
ListNode* dummy = new ListNode();
23+
dummy->next=head;
24+
int i=0;
25+
// if(head != NULL)
26+
// {
27+
// dummy->next=head;
28+
// }
29+
ListNode* pre=dummy;
30+
for (i=0;i<left-1;++i)
31+
{
32+
33+
pre=pre->next;
34+
}
35+
ListNode* curr = pre->next;
36+
ListNode* cn=nullptr;
37+
for (i=0;i<right-left;++i)
38+
{
39+
cn=curr->next;
40+
curr->next=cn->next;
41+
cn->next=pre->next;
42+
pre->next=cn;
43+
}
44+
return dummy->next;
45+
}
46+
};

0 commit comments

Comments
 (0)