国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[Leetcode] 21. Merge Two Sorted Lists

2019-11-08 02:00:38
字體:
來源:轉載
供稿:網友

PRoblem:

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.

Idea: Basic singly-linked list Operation. Use two ListNodes as pointers to point two input lists individually. Compare values of two ListNodes and then make the ListNode with smaller value move backward.

Solution:

# 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): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ inode = l1 jnode = l2 if l1 == None: head = l2 return head elif l2 == None: head = l1 return head elif l1.val < l2.val: head = l1 inode = inode.next else: head = l2 jnode = jnode.next tmpnode = head while inode != None or jnode != None: if inode == None: tmpnode.next = jnode return head elif jnode == None: tmpnode.next = inode return head elif inode.val < jnode.val: tmpnode.next = inode tmpnode = tmpnode.next inode = inode.next else: tmpnode.next = jnode tmpnode = tmpnode.next jnode = jnode.next return head
上一篇:cf#302 C. Writing Code dp

下一篇:小鑫的城堡

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 耿马| 千阳县| 鲁甸县| 潼南县| 定州市| 阿合奇县| 元谋县| 阳春市| 阿克苏市| 抚远县| 北票市| 双城市| 容城县| 梁平县| 牙克石市| 时尚| 宜昌市| 苗栗市| 上思县| 托克逊县| 个旧市| 浦东新区| 宜川县| 绵阳市| 望谟县| 体育| 原阳县| 上林县| 武胜县| 金阳县| 望谟县| 姜堰市| 耒阳市| 乌鲁木齐县| 龙胜| 沅陵县| 吉木萨尔县| 云梦县| 哈巴河县| 时尚| 盐津县|