原创

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

题目描述

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

解题思路

两种解法:递归和非递归

参考代码

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
正文到此结束
本文目录