合并两个已排序的链表,新链表中的每一个节点必须是来自输入的两个链表的节点(即不能构造新的节点),返回新链表的头部。node
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.git
example 1github
input: 1->2->4, 3->8 output: 1->2->3->4->8
head
指向输入两个链表中头节点较小值,做为新链表的头部code
tail
指向新链表表尾,初始状态head = tail
排序
a
扫描l1
,b扫描l2
,比较a
和b
节点内值的大小,将较小的加入tail
以后,a
和b
中较小的向后移动一个节点,较大的不动,tail向后移动一个节点保证任意时候指向都是新链表尾部ci
l1
和l2
其中一个已经遍历完,若另外一个还有元素,添加到tail
以后leetcode
# Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x self.next = None class Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if None in (l1, l2): return l1 or l2 head = tail = l1 if l1.val <= l2.val else l2 a = l1 if l1.val > l2.val else l1.next b = l2 if l1.val <= l2.val else l2.next while a and b: if a.val <= b.val: tail.next = a tail, a = tail.next, a.next else: tail.next = b tail, b = tail.next, b.next tail.next = a or b return head
本题以及其它leetcode题目代码github地址: github地址get