LeetCode-1669题:合并两个链表(原创)

【题目描述】

        给你两个链表 list1 和 list2 ,它们包含的元素分别为 n 个和 m 个。请你将 list1 中下标从 a 到 b 的全部节点都删除,并将list2 接在被删除节点的位置。下图中蓝色边和节点展示了操作后的结果:

请你返回结果链表的头指针。

【提示】

1)3 <= list1.length <= 104
2)1 <= a <= b < list1.length - 1
3)1 <= list2.length <= 104

【题目链接】. - 力扣(LeetCode)

【解题代码】

package list;

import list.base.ListNode;

public class MergeInBetween {

    public static void main(String[] args) {
        int[] l1 = new int[]{0, 1, 2, 3, 4, 5, 6};
        int[] l2 = new int[]{1000000, 1000001, 1000002, 1000003, 1000004};
        ListNode list1 = ListNode.makeList(l1);
        ListNode list2 = ListNode.makeList(l2);

        ListNode list3 = new MergeInBetween().mergeInBetween(list1, 2, 5, list2);
        list3.printList();
    }

    private ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        // 先从list1的首节点走a-1步找到节点a前一节点
        ListNode preANode = followingNode(list1, a - 1);
        // 再从a节点走b-a+2步找到节点b+1
        ListNode nextBNode = followingNode(preANode, b - a + 2);
        // 找到list2的尾节点
        ListNode tailNode2 = getTailNode(list2);
        // 将节点a下一节点指向list2首节点
        preANode.next = list2;
        // 将list2的尾节点指向节点b下一个节点
        tailNode2.next = nextBNode;
        return list1;
    }

    private ListNode followingNode(ListNode node, int step) {
        int i = 0;
        ListNode node2 = node;
        while (i < step) {
            node2 = node2.next;
            i++;
        }
        return node2;
    }

    private ListNode getTailNode(ListNode node) {
        while (node.next != null) {
            node = node.next;
        }
        return node;
    }
}

【解题思路】

     根据题目描述,可以得出链表操作完之后:

  1. a节点的前一节点指向list2的首节点
  2. list2的尾节点指向b的下一节点 

根据上述思路,很快完成代码编写,并提交LeetCode成功

        

【解题步骤】

  1.  定义一个函数followingNode,从链表某一节点,向后走step步
    private ListNode followingNode(ListNode node, int step) {
    	int i = 0;
    	ListNode node2 = node;
    	while (i < step) {
    	    node2 = node2.next;
    	    i++;
    	}
    	return node2;
    }
  2. 定义一个函数getTailNode,找到链表尾节点 
    private ListNode getTailNode(ListNode node) {
        while (node.next != null) {
            node = node.next;
        }
        return node;
    }
  3. 先从list1的首节点走a-1步找到节点a前一节点
     ListNode preANode = followingNode(list1, a - 1);
  4. 再从a节点走b-a+2步找到节点b+1
    ListNode nextBNode = followingNode(preANode, b - a + 2);
  5. 找到list2的尾节点
    ListNode tailNode2 = getTailNode(list2);
  6. 将节点a下一节点指向list2首节点
    preANode.next = list2
  7. 将list2的尾节点指向节点b下一个节点
    tailNode2.next = nextBNode
  8. 最后返回链表list1首节点即可
    return list1;

【思考总结】

  1. 链表操作要注意首节点保存和尾节点的获取与保存
  2. 所有链表操作基本上都是对三个变量的操作:当前节点curNode,上一节点preNode,下一节点nextNode;
  3. 链表遍历时对不停地更新上面三个变量
  4. LeetCode解题之前,一定不要看题解,看了就“破功”了! 
  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要求合并两个有序链表。对于链表中的每一个节点,由于链表是有序的,所以可以将两个链表中的节点按照大小顺序进行比较,然后逐个将较小的节点链接上去,最终得到一个新的有序链表。 使用Rust语言实现这个问,需要首先定义一个链表节点的结构体,包含node值以及next指针。然后定义一个函数来合并两个有序链表,输入为两个链表的头节点指针,输出为新的有序链表的头节点指针。 在合并过程中,首先需要判断两个链表的头节点哪一个较小,将较小的节点作为新链表的头节点,并将该节点的next指针指向递归调用合并函数的结果。递归结束条件为其中一个链表为空,则将另一个链表直接链接到新链表上。 完整代码如下: ```rust // 定义链表节点结构体 #[derive(Debug)] struct ListNode { val: i32, next: Option<Box<ListNode>>, } impl ListNode { fn new(val: i32) -> Self { ListNode { val, next: None } } } // 合并两个有序链表 fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { match (l1, l2) { (None, None) => None, // 两个链表均为空 (l1, None) => l1, // 其中一个链表为空,直接返回另一个链表 (None, l2) => l2, (Some(mut l1), Some(mut l2)) => { if l1.val < l2.val { l1.next = merge_two_lists(l1.next, Some(l2)); Some(l1) } else { l2.next = merge_two_lists(Some(l1), l2.next); Some(l2) } } } } // 测试代码 fn main() { let l1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let l2 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let merged = merge_two_lists(l1, l2); println!("{:?}", merged); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值