国产探花免费观看_亚洲丰满少妇自慰呻吟_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

下一篇:小鑫的城堡

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平远县| 济阳县| 水城县| 临湘市| 定陶县| 马关县| 库尔勒市| 南郑县| 买车| 西青区| 山阳县| 永川市| 陆丰市| 林甸县| 临汾市| 旬邑县| 乐山市| 敦化市| 高密市| 府谷县| 屯留县| 万宁市| 藁城市| 长白| 元氏县| 兴隆县| 如皋市| 桐城市| 阿城市| 额济纳旗| 保定市| 资讯 | 平武县| 诏安县| 淅川县| 舞阳县| 房山区| 宿松县| 奉新县| 祁阳县| 贵阳市|