【python】链表的遍历

先定义链表结点类

# 定义结点类
class Node:
	def __init__(self, val):
		self.val = val
		self.next = None

	def traverse(self):
		cur = self
		res = []
		while cur is not None:
			print(cur.val)
			res.append(cur)
			cur = cur.next
		return res

猜你喜欢

转载自www.cnblogs.com/vivlalib/p/12555786.html
今日推荐