File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @param {number } left
11
+ * @param {number } right
12
+ * @return {ListNode }
13
+ */
14
+ const reverseBetween = function ( head , left , right ) {
15
+ if ( head == null ) return head
16
+ const dummy = new ListNode ( null , head )
17
+ let num = 0 , cur = head
18
+ while ( cur ) {
19
+ num ++
20
+ cur = cur . next
21
+ }
22
+ let idx = 0 , pre = null
23
+ cur = dummy
24
+ while ( idx < right && idx <= num ) {
25
+ if ( idx === left - 1 ) pre = cur
26
+ if ( idx >= left ) {
27
+ const tmp = pre . next
28
+ pre . next = cur . next
29
+ cur . next = cur . next . next
30
+ pre . next . next = tmp
31
+ }
32
+
33
+ if ( idx < left ) cur = cur . next
34
+ idx ++
35
+ }
36
+ return dummy . next
37
+ } ;
38
+
39
+ // another
40
+
1
41
/**
2
42
* Definition for singly-linked list.
3
43
* function ListNode(val) {
You can’t perform that action at this time.
0 commit comments