92. Reverse Linked List II Posted on 2021-01-10 Edited on 2021-01-25 In LeetCode Disqus: Symbols count in article: 707 Reading time ≈ 1 mins. O(n) time O(1) space这道题的思路跟普通的reverse不一样!!curr是固定的,永远指向初始的第m个结点,anchor也是固定的,永远是curr的前继结点每次curr都是和它的succ交换,然后让next的下一个指向anchor的下一个结点,最后anchor的下一个指向那个succ结点 1234567891011121314151617181920212223242526/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode dummy_head(0), *anchor = &dummy_head; dummy_head.next = head; n -= m; while (--m > 0) anchor = anchor->next; auto curr = anchor->next; while (n-- > 0) { auto succ = curr->next; curr->next = succ->next; succ->next = anchor->next; anchor->next = succ; } return dummy_head.next; }};