LeetCode-92. Reverse Linked List II
问题描述
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
1 | Input: head = [1,2,3,4,5], left = 2, right = 4 |
Constraints:
- The number of nodes in the list is
n. 1 <= n <= 500-500 <= Node.val <= 5001 <= left <= right <= n
解答
需要注意的是,在表头之前额外增加了一个节点res,res的next指向表头,这样是为了更方便得处理从表头开始反转时的情况。
1 | /* |