题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
解法一:循环法
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回合并后列表
def Merge(self, l1, l2):
# write code here
sentinel = ListNode(0)
l = sentinel
while l1 and l2:
if l1.val > l2.val:
l.next, l2 = l2, l2.next
else:
l.next, l1 = l1, l1.next
l = l.next
if l1:
l.next = l1
if l2:
l.next = l2
return sentinel.next