LeetCode092——反转链表II

本文深入解析LeetCode 206 题目“反转链表”的两种解题思路:递归实现与非递归实现。详细介绍了寻找待反转子链表起止节点的方法,提供了JAVA代码示例,帮助读者理解和掌握链表的反转技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/description/

题目描述:

知识点:链表

思路一:递归实现

本题只是在LeetCode206——反转链表的基础上外加了一个寻找待反转子链表的第一个节点的前一个节点以及最后一个节点的过程而已。根据LeetCode206——反转链表的思路,有递归实现和非递归实现两种方法。

由于涉及到递归,而每一次递归的时间复杂度都是O(1)级别的,因此时间复杂度和空间复杂度都是O(n - m)。

JAVA代码:

public class Solution {

    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode cur1 = dummyHead;
        int interval = n - m;
        while(m > 1){
            m--;
            cur1 = cur1.next;
        }
        ListNode cur2 = cur1.next;
        while(interval > 0){
            interval--;
            cur2 = cur2.next;
        }
        ListNode temp1 = cur1.next;
        ListNode temp2 = cur2.next;
        cur2.next = null;
        cur1.next = reverse(temp1);
        temp1.next = temp2;
        return dummyHead.next;
    }

    private ListNode reverse(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode newHead = reverse(head.next);
        ListNode temp = head.next;
        temp.next = head;
        head.next = null;
        return newHead;
    }
}

LeetCode解题报告:

思路二:非递归实现

寻找待反转子链表的第一个节点的前一个节点以及最后一个节点的过程与思路一相同,反转链表的过程与LeetCode206——反转链表中的非递归实现相同。

时间复杂度是O(m - n),空间复杂度是O(1)。

JAVA代码:

public class Solution {

    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode cur1 = dummyHead;
        int interval = n - m;
        while(m > 1){
            m--;
            cur1 = cur1.next;
        }
        ListNode cur2 = cur1.next;
        while(interval > 0){
            interval--;
            cur2 = cur2.next;
        }
        ListNode temp1 = cur1.next;
        ListNode temp2 = cur2.next;
        cur2.next = null;
        cur1.next = reverse(temp1);
        temp1.next = temp2;
        return dummyHead.next;
    }

    private ListNode reverse(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode cur1 = dummyHead;
        ListNode cur2 = dummyHead.next;
        ListNode cur3 = cur2.next;
        while(cur3 != null){
            cur2.next = cur3.next;
            ListNode temp = cur1.next;
            cur1.next = cur3;
            cur3.next = temp;
            cur3 = cur2.next;
        }
        return dummyHead.next;
    }
}

LeetCode解题报告:

 

参考资源链接:[C++解决LeetCode第86题:分隔链表题解](https://wenku.csdn.net/doc/2v3shkrvm5?utm_source=wenku_answer2doc_content) 在解决LeetCode第86题——分隔链表的过程中,掌握C++语言和链表操作是关键。为了帮助你更好地理解并实现这一题目的解决方案,特别推荐《C++解决LeetCode第86题:分隔链表题解》这份资源。通过这份资料,你可以获得关于本题题解的详细解析和代码实现,直接应对当前的编程挑战。 分隔链表题要求将链表中所有小于给定值x的节点移动到所有大于等于x的节点之前,同时保持原有节点的相对顺序。以下是使用C++实现的步骤和代码示例: 1. 创建两个虚拟头节点,分别为small和large,这有助于处理边界条件,简化链表的插入操作。 2. 遍历原始链表,根据节点值与x的比较结果,将节点分别插入到small和large链表中。 3. 将small链表的最后一个节点连接到large链表的第一个节点,完成链表的分隔。 4. 返回small链表的头节点作为最终链表的头节点。 以下是C++代码实现: ```cpp // 定义链表节点结构 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; // 分隔链表函数实现 ListNode* partition(ListNode* head, int x) { ListNode dummySmall(0), dummyLarge(0); ListNode *small = &dummySmall, *large = &dummyLarge; while (head) { if (head->val < x) { small = small->next = head; } else { large = large->next = head; } head = head->next; } small->next = dummyLarge.next; large->next = nullptr; return dummySmall.next; } ``` 在掌握上述分隔链表的解法后,建议进一步深入学习链表操作的各种技巧,包括反转链表、合并链表、检测环等,这将对提升你的数据结构和算法水平大有裨益。为了继续扩展你的知识,不妨探索更多类似的C++题解和算法实践资源,这会加深你对LeetCode平台以及编程面试准备的理解。 参考资源链接:[C++解决LeetCode第86题:分隔链表题解](https://wenku.csdn.net/doc/2v3shkrvm5?utm_source=wenku_answer2doc_content)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值