File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-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+
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+ };
You can’t perform that action at this time.
0 commit comments