Skip to content

Instantly share code, notes, and snippets.

@jack5241027
Created April 21, 2019 09:20
Show Gist options
  • Select an option

  • Save jack5241027/d04504455dd02679d4df8371d446f15f to your computer and use it in GitHub Desktop.

Select an option

Save jack5241027/d04504455dd02679d4df8371d446f15f to your computer and use it in GitHub Desktop.
92. Reverse Linked List II (js)
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} m
* @param {number} n
* @return {ListNode}
*/
var reverseBetween = function(head, m, n) {
if (head === null) return head;
const dummy = new ListNode(0);
dummy.next = head;
head = dummy;
for (let i = 0; i < m - 1; i++) {
head = head.next;
}
const preNode = head;
const n1 = head.next;
let nk = head.next;
let nkPlus = nk.next;
while (m < n) {
const temp = nkPlus.next;
nkPlus.next = nk;
nk = nkPlus;
nkPlus = temp;
m += 1;
}
preNode.next = nk;
n1.next = nkPlus;
return dummy.next;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment