栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用python实现单链表

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python实现单链表

Get the value of the index-th node in the linked list. If the index is invalid, return -1. :type index: int :rtype: int if index 0 or index self.length(): return -1 curnode self.head if index 0: return curnode.value tag 1 while curnode.next: curnode curnode.next if tag index: return curnode.value tag 1 def addAtHead(self, val): Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. :type val: int :rtype: None if self.head is None: curnode Node(val) self.head curnode # self.printlink() return curnode Node(val) curnode.next self.head self.head curnode # self.printlink() return def addAtTail(self, val): Append a node of value val to the last element of the linked list. :type val: int :rtype: None if self.head is None: self.head Node(val) # self.printlink() return curnode self.head if curnode.next is None: curnode.next Node(val) self.printlink() return while curnode.next: curnode curnode.next curnode.next Node(val) # self.printlink() return def addAtIndex(self, index, val): Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. :type index: int :type val: int :rtype: None if index 0 or index self.length(): return -1 if index self.length(): self.addAtTail(val) return if index 0: self.addAtHead(val) return tag 1 curnode self.head node Node(val) while curnode.next: if tag index: node.next curnode.next curnode.next node curnode curnode.next tag 1 # self.printlink() return def deleteAtIndex(self, index): Delete the index-th node in the linked list, if the index is valid. :type index: int :rtype: None if index 0 or index self.length(): return -1 if index 0: self.head self.head.next # self.printlink() return curnode self.head if index self.length() - 1: while curnode.next.next is not None: curnode curnode.next curnode.next None # self.printlink() return tag 1 while curnode.next: if tag index: curnode.next curnode.next.next curnode curnode.next tag 1 # self.printlink() return def length(self): if self.head is None: return 0 length 1 curnode self.head while curnode.next: length 1 curnode curnode.next return length def getvalue(self): ele [] curnode self.head if self.head is not None: for i in range(0, self.length()): ele.append(curnode.value) curnode curnode.next return ele else: return -1 def printlink(self): print( 链表长度为: str(self.length()), 链表为: str(self.getvalue())) linkedList MylinkedList() linkedList.addAtHead(1) linkedList.addAtTail(3) linkedList.addAtIndex(1, 2) print(linkedList.get(1)) linkedList.deleteAtIndex(1) print(linkedList.get(1))

为了清晰了解对链表每一步的操作情况 我写了getvalue 函数。

总结

我在提交代码的时候 好几次因为超时 我就很郁闷 检查了很多遍 最后发现问题出在self.printlink()这里 代码中我注释的那个方法 如果想要查看每一步的操作步骤 取消注释即可

我把self.printlink()全部注释掉 所用的时长大约是3500ms 加一个self.printlink()用的时长大约是3800ms。再加一个self.printlink()就超过4000ms了。因此我直接把全部的self.printlink()都注释了 要详细查看的话只需打开即可。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268302.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号