美文网首页皮皮的LeetCode刷题库
【剑指Offer】016——合并两个排序的链表 (链表)

【剑指Offer】016——合并两个排序的链表 (链表)

作者: 就问皮不皮 | 来源:发表于2019-08-18 22:59 被阅读0次

题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

解题思路

两种解法:递归和非递归

参考代码

class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}

public class Solution {
    // 递归的方法
    public ListNode Merge(ListNode list1, ListNode list2) {
        if (list1 == null) return list2;
        if (list2 == null) return list1;
        ListNode mergehead = null;  // 融合后的头
        if (list1.val <= list2.val) {
            mergehead = list1;
            mergehead.next = Merge(list1.next, list2);
        } else {
            mergehead = list2;
            mergehead.next = Merge(list1, list2.next);
        }
        return mergehead;
    }
    // 非递归方法
    public ListNode Merge2(ListNode list1, ListNode list2) {
        if (list1 == null) return list2;
        else if (list2 == null) return list1;
        ListNode mergehead = null;
        // 确定头
        if (list1.val <= list2.val) {
            mergehead = list1;
            list1 = list1.next;
        } else {
            mergehead = list2;
            list2 = list2.next;
        }
        ListNode cur = mergehead;   // 原始的头被保存
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                cur.next = list1;
                list1 = list1.next;
            } else {
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        // 避免一个走完了,一个还没走完
        if (list1 != null) {
            cur.next = list1;
        } else if (list2 != null) {
            cur.next = list2;
        }
        return mergehead;
    }
}
# -*- coding:utf-8 -*-
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        if pHead1 is None:
            return pHead2
        elif pHead2 is None:
            return pHead1
        mergehead = None
        
        if pHead1.val <= pHead2.val:
            mergehead = pHead1
            mergehead.next = self.Merge(pHead1.next, pHead2)
        else:
            mergehead = pHead2
            mergehead.next = self.Merge(pHead1, pHead2.next)
        return mergehead
    def Merge2(self, pHead1, pHead2):
        if pHead1 is None:
            return pHead2
        elif pHead2 is None:
            return pHead1
        mergeHead = None
        if pHead1.val <= pHead2.val:
            mergeHead = pHead1
            pHead1 = pHead1.next
        else:
            mergeHead = pHead2
            pHead2 = pHead2.next
        cur = mergeHead     # 保留头部,供返回
        while pHead1 is not None and pHead2 is not None:
            if pHead1.val <= pHead2.val:
                cur.next = pHead1
                pHead1 = pHead1.next
            else:
                cur.next = pHead2
                pHead2 = pHead2.next
            cur = cur.next
        if pHead1 is not None:
            cur.next = pHead1
        elif pHead2 is not None:
            cur.next = pHead2
        return mergeHead

个人订阅号

image

相关文章

网友评论

    本文标题:【剑指Offer】016——合并两个排序的链表 (链表)

    本文链接:https://www.haomeiwen.com/subject/rptjsctx.html