while (cur != nullptr) { ListNode *next = cur->next; cur->next = pre; pre = cur; cur = next; } }
public: ListNode *reverseBetween(ListNode *head, int left, int right){ // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论 ListNode *dummyNode = newListNode(-1); dummyNode->next = head;
ListNode *pre = dummyNode; // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点 // 建议写在 for 循环里,语义清晰 for (int i = 0; i < left - 1; i++) { pre = pre->next; }
// 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点 ListNode *rightNode = pre; for (int i = 0; i < right - left + 1; i++) { rightNode = rightNode->next; }
classSolution: defreverseBetween(self, head: ListNode, left: int, right: int) -> ListNode: defreverse_linked_list(head: ListNode): # 也可以使用递归反转一个链表 pre = None cur = head while cur: next = cur.next cur.next = pre pre = cur cur = next
# 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论 dummy_node = ListNode(-1) dummy_node.next = head pre = dummy_node # 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点 # 建议写在 for 循环里,语义清晰 for _ inrange(left - 1): pre = pre.next
# 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点 right_node = pre for _ inrange(right - left + 1): right_node = right_node.next # 第 3 步:切断出一个子链表(截取链表) left_node = pre.next curr = right_node.next
structListNode *pre = dummyNode; // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点 // 建议写在 for 循环里,语义清晰 for (int i = 0; i < left - 1; i++) { pre = pre->next; }
// 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点 structListNode *rightNode = pre; for (int i = 0; i < right - left + 1; i++) { rightNode = rightNode->next; }
classSolution { public ListNode reverseBetween(ListNode head, int left, int right) { // 设置 dummyNode 是这一类问题的一般做法 ListNodedummyNode=newListNode(-1); dummyNode.next = head; ListNodepre= dummyNode; for (inti=0; i < left - 1; i++) { pre = pre.next; } ListNodecur= pre.next; ListNode next; for (inti=0; i < right - left; i++) { next = cur.next; cur.next = next.next; next.next = pre.next; pre.next = next; } return dummyNode.next; } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defreverseBetween(self, head: ListNode, left: int, right: int) -> ListNode: # 设置 dummyNode 是这一类问题的一般做法 dummy_node = ListNode(-1) dummy_node.next = head pre = dummy_node for _ inrange(left - 1): pre = pre.next
cur = pre.next for _ inrange(right - left): next = cur.next cur.next = next.next next.next = pre.next pre.next = next return dummy_node.next
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcreverseBetween(head *ListNode, left, right int) *ListNode { // 设置 dummyNode 是这一类问题的一般做法 dummyNode := &ListNode{Val: -1} dummyNode.Next = head pre := dummyNode for i := 0; i < left-1; i++ { pre = pre.Next } cur := pre.Next for i := 0; i < right-left; i++ { next := cur.Next cur.Next = next.Next next.Next = pre.Next pre.Next = next } return dummyNode.Next }
structListNode *pre = dummyNode; for (int i = 0; i < left - 1; i++) { pre = pre->next; } structListNode *cur = pre->next; structListNode *next; for (int i = 0; i < right - left; i++) { next = cur->next; cur->next = next->next; next->next = pre->next; pre->next = next; } return dummyNode->next; }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var reverseBetween = function(head, left, right) { // 设置 dummyNode 是这一类问题的一般做法 const dummy_node = newListNode(-1); dummy_node.next = head; let pre = dummy_node; for (let i = 0; i < left - 1; ++i) { pre = pre.next; }
let cur = pre.next; for (let i = 0; i < right - left; ++i) { const next = cur.next; cur.next = next.next; next.next = pre.next; pre.next = next; } return dummy_node.next; };