在做leetcode中和链表相关的题目时,不需要自己写输入输出,但是我们在进行本地调试的时候需要自己编写输入输出代码,完整代码如下:
# Definition for singly-linked list. class ListNode: def __init__(self, x):
self.val = x self.next = None class LinkList: def __init__(self):
self.head=None def initList(self, data): # 创建头结点 self.head = ListNode(data[0])
r=self.head p = self.head # 逐个为 data 内的数据创建结点, 建立链表 for i in data[1:]: node =
ListNode(i) p.next = node p = p.next return r def printlist(self,head): if head
== None: return node = head while node != None: print(node.val,end=' ') node =
node.next class Solution: def mergeTwoLists(self, l1, l2): """ :type l1:
ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode(0) first =
head while l1!=None and l2!=None: if l1.val <= l2.val: head.next = l1 l1 =
l1.next else: head.next = l2 l2 = l2.next head = head.next if l1 != None:
head.next = l1 elif l2 != None: head.next = l2 return first.next if __name__ ==
'__main__': a = Solution() l=LinkList() data1 = [1, 2, 3] data2= [2, 4, 6]
l1=l.initList(data1) l2=l.initList(data2) l.printlist(l1) print("\r")
l.printlist(l2) print("\r") m=a.mergeTwoLists(l1,l2) l.printlist(m)
这些代码很重要!!!!

技术
今日推荐
阅读数 169685
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信