# LeetCode: Reverse Linked List II Solution

## Approach

I found it hard to understand one-pass solution, so I go with the so-called cheated solution

• Convert to linked list to array
• Manipulate on that array
• Convert back to linked list

## Complexity

Time:

O(n)

Space:

O(n)

with

n
is the length of the list

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**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} head10 * @param {number} left11 * @param {number} right12 * @return {ListNode}13 */14var reverseBetween = function (head, left, right) {15  left--16  right--17  const arr = []18
24  const resArr = [25    ...arr.slice(0, left),26    ...arr.slice(left, right + 1).reverse(),27    ...arr.slice(right + 1),28  ]29
30  for (let i = 0; i < resArr.length - 1; i++) {31    resArr[i].next = resArr[i + 1]32  }33
34  return resArr[0]35}```

leetcode

## Next Post

LeetCode: Remove All Adjacent Duplicates In String

Jun 29, 2021

Remove all adjacent and equal letters

Search Posts