# [LeetCode 92. Reverse Linked List II ](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/606/week-4-june-22nd-june-28th/3789/) ### Daily challenge Jun 23, 2021 (EASY) >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. ![](https://i.imgur.com/5IkAzfD.png) :::info **Example 1:** **Input:** head = [1,2,3,4,5], left = 2, right = 4 **Output:** [1,4,3,2,5] ::: :::info **Example 2:** **Input:** head = [5], left = 1, right = 1 **Output:** [5] ::: :::warning **Constraints:** * The number of nodes in the list is n. * 1 <= n <= 500 * -500 <= Node.val <= 500 * 1 <= left <= right <= n ::: --- ### Approach 1 : Dummy Node :book: **`0 ms ( % )`** **`O()`** 使 **`dummy`** 指向 **`head`**,另外宣告三個 pointer **`( p, start, end )`** 分別表示 `node before start` 、 `a pointer to the beginning of a sub-list that will be reversed` 、 `a pointer to a node that will be reversed`,接下來進行 reverse。 ```cpp=18 for(int i=left; i<right; i++){ start->next = end->next; // (a) end->next = p->next; // (b) p->next = end; // (c) end = start->next; // (d) } ``` ![](https://i.imgur.com/zwG5tW0.png) ```cpp=1 class Solution { public: ListNode* reverseBetween(ListNode* head, int left, int right) { if(head == NULL || left == right) return head; ListNode* dummy = new ListNode(0, head); ListNode* p = dummy; ListNode* start; ListNode* end; for(int i=1; i<left; i++){ p = p->next; } start = p->next; end = start->next; for(int i=left; i<right; i++){ start->next = end->next; // (a) end->next = p->next; // (b) p->next = end; // (c) end = start->next; // (d) } return dummy->next; } }; ```