python listnode(0)_Python——链表的输入输出代码,python

在做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: L


版权声明:本文为weixin_39932762原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。