-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcodeRecursive.js
33 lines (28 loc) · 1.09 KB
/
leetcodeRecursive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// leetcode solution translated to Javascript
// this solution swaps node values, does not manipulate next pointers
const reverseBetween = function(head, m, n) {
this.left = head;
this.continue = true;
recurseAndReverse(head, m, n);
return head;
};
const recurseAndReverse = function(right, m, n) {
if (n === 1) return;
right = right.next;
if (m > 1) this.left = this.left.next;
recurseAndReverse(right, m - 1, n - 1);
// If the pointers cross each other or become equal,
// there is nothing else to swap
if (this.left === right || right.next === this.left) this.continue = false;
// Until the boolean stop is true, swap data between the two pointers
// When boolean is false, nothing happens and function resolves,
// since this.stop = true, function keeps resolving/returning until stack is empty
if (this.continue) {
const temp = this.left.val;
this.left.val = right.val;
right.val = temp;
// Move left one step to the right.
this.left = this.left.next;
// The right pointer doesn't need to be moved, it is pointing at previous in the previous call
}
};