题目:
题解:
struct ListNode *reverseBetween(struct ListNode *head, int left, int right) {// 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论struct ListNode *dummyNode = malloc(sizeof(struct ListNode));dummyNode->val = -1;dummyNode->next = head;struct ListNode *pre = dummyNode;for (int i = 0; i < left - 1; i++) {pre = pre->next;}struct ListNode *cur = pre->next;struct ListNode *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;
}