leetcode Reverse Linked List II

来源:互联网 发布:cdx什么意思网络用语 编辑:程序博客网 时间:2024/06/07 05:39

多个指针,让cur倒过来得到结果

/** * 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 *root = new ListNode(0);        root->next = head;        ListNode *p = root;        for(int i = 0; i < m-1; i++)            p = p->next;        ListNode *h = p;        p = h->next;        ListNode *cur = p->next;        for(int i = m; i < n; i++){            p->next = cur->next;            cur->next =h->next;            h->next = cur;            cur = p->next;        }        return root->next;    }};


0 0