21. Merge Two Sorted Lists

来源:互联网 发布:qq堂能不能再mac上运行 编辑:程序博客网 时间:2024/06/05 12:50

题目描述:
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

思路:
两链表不一定等长,先遍历比较两链表都不为空时的最小值存入新链表,当某一链表下一个值为空时,将另一链表剩余部分存入新链表

当前解决:

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def mergeTwoLists(self, l1, l2):        l3 = ListNode(0)        p = l3        while l1 and l2:            if l1.val < l2.val:                p.next = l1                l1 = l1.next            else:                p.next = l2                l2 = l2.next            p = p.next        if l1:            p.next = l1        else:            p.next = l2        return l3.next
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 龙生蛭 蛭蛇通络胶囊 蛭石可以用沙子代替吗 蛭石的用途 蛭石的作用 水泥蛭石 蛭石破碎机 蛭石哪里挖 蛭石价格 蛭石金属瓦 蛭石批发 灵寿蛭石 蛭石粉价格 水泥蛭石价格 蛭石珍珠岩 蛭蛇通络胶囊多少钱一盒 蛭子 至石 蛭石图片 什么是蛭石 灵寿蛭石厂家 种植蛭石 蛭石粉多少钱 跖石 膨胀蛭石 蛭石厂家 蛭石粉厂家 蛭石多少钱一斤 蛭石混凝土 蛭石和珍珠岩哪个好 蛭石矿 草炭蛭石 蛭石营养土 园艺用蛭石 加工蛭石设备 蛭石粉的用途 蛭石粉的作用 蛭石加工设备 横蛮 蛮吉 蛮吉的口头禅