-
-
Save jack5241027/d04504455dd02679d4df8371d446f15f to your computer and use it in GitHub Desktop.
92. Reverse Linked List II (js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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