【leetcode】92. 反转链表 II

 

struct ListNode* reverseBetween(struct ListNode* head, int m, int n){
    if (m == n) return head;
    struct ListNode* root = (struct ListNode*)calloc(sizeof(struct ListNode), 1);    
    struct ListNode* left, *next, *pre=root;
    root->next = head;
    int cnt = 0;
    while (++cnt <= n){
        next = head->next;
        if (cnt > m && cnt <= n){        
            head->next = pre;
        }
        else if (cnt == m)
            left = pre;
        pre = head;
        head = next;
    }
    left->next->next = head;
    left->next = pre;
    return root->next;
}

 

posted @ 2020-12-14 00:57  温暖了寂寞  阅读(68)  评论(0编辑  收藏  举报