File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ ListNode* reverseBetween(ListNode* head, int left, int right) {
14
+ if (!head || left == right) {
15
+ return head;
16
+ }
17
+
18
+ ListNode* dummy = new ListNode(0);
19
+ dummy->next = head;
20
+ ListNode* prev = dummy;
21
+
22
+ for (int i = 0; i < left - 1; i++) {
23
+ prev = prev->next;
24
+ }
25
+
26
+ ListNode* cur = prev->next;
27
+
28
+ for (int i = 0; i < right - left; i++) {
29
+ ListNode* temp = cur->next;
30
+ cur->next = temp->next;
31
+ temp->next = prev->next;
32
+ prev->next = temp;
33
+ }
34
+
35
+ return dummy->next;
36
+ }
37
+ };
You can’t perform that action at this time.
0 commit comments