首页 > 其他 > 详细

leetcode-algorithms-92. Reverse Linked List II

时间:2019-06-01 22:38:53      阅读:76      评论:0      收藏:0      [点我收藏+]

leetcode-algorithms-92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ m ≤ n ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL

解法

/**
 * 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 d(-1);
        d.next = head;
        ListNode *pre_first = &d;
        for (int i = 1; i < m; ++i) {
            pre_first = pre_first->next;
        }
        
        ListNode *first = pre_first->next;
        ListNode *second;
        ListNode *second_last;
        if (first->next != nullptr)
            second = first->next;
        
        for (int i = 0; i < n - m; ++i) {
            second_last = second->next;
            second->next = pre_first->next;
            pre_first->next = second;
            first->next = second_last;
            second = second_last;
        }
        
        return d.next;  
    }
};

时间复杂度: O(n),n 反转的的结点 index 值。

空间复杂度: O(1)。

leetcode-algorithms-92. Reverse Linked List II

原文:https://www.cnblogs.com/mathli/p/10961246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!