python 链表
在C/C++中,通常采用“指针+结构体”来实现链表;而在Python中,则可以采用“引用+类”来实现链表。
节点类:
class Node:
def __init__(self, data):
self.data = data
self.next = None
链表类:
class Linkedlist:
def __init__(self):
self.head = None
self.tail = None
link_list = LinkedList()
def is_empty(self):
return self.head is None
def append(self, data):
node = Node(data)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail =node
def iter(self):
if not iter.head:
return
cur = self.head
yield cur.data
while cur.next:
cur = cur.next
yield cur.data
#先判断是不是空链表,yield head.data 再用while循环遍历
链表的头结点head 和 尾节点tail 都属于node.
insert:先将要插入的节点的next指向之后链表的head,然后将之前链表的next指向 将要插入的节点。
def insert(self, idx, value):
cur = self.head
cur_idx = 0
if cur is None:
raise Exception('That list is and empty list!')
while cur_idx < idx-1:
cur = cur.next
if cur is None:
raise Exception('List length less than index!')
cur_idx += 1
node = Node(value)
node.next = cur.next
cur.next = node
if node.next is None:
self.tail = node
def remove(self, idx):
cur = self.head
cur_idx = 0
#空指针
if self.head = None:
raise Exception('This is an empty list')
while cur_idx < idx-1:
cur = cur.next
#给出的索引大于链表的长度
if cur is None:
raise Exception('list length less than index')
cur_idx +=1
if idx == 0: #当删除第一个节点时
self.head = cur.next
cur = cur.next
return
if self.head is self.tail: #当只有一个节点时
self.head = None
self.tail = None
return
cur.next = cur.next.next
if cur.next is None: #当删除最后一个节点时
self.tail = cur
def size(self):
i = 0
cur = self.head
if current is None:
return 'The list is an empty list'
while cur.next is not None:
i +=1
cur = cur.next
return i
def search(self, item):
current = self.head
found = False
while current is not None and not found:
if current.data == item:
found = True
else:
current = current.next
return found
1,迭代
# -*- coding: utf-8 -*-
#!/bin/env python
# Python2.7 class Node(object):
def __init__(self):
self.value = None
self.next = None
def __str__(self):
return str(self.value) def reverse_list(head):
if not head or not head.next:
return head
pre = None
while head:
next = head.next # 缓存当前节点的向后指针,待下次迭代用
head.next = pre # 关键:把当前节点向前指针(pre)作为当前节点的向后指针
pre = head # 把当前指针赋值给 下次迭代 节点的 向前指针
head = next # 作为下次迭代时的(当前)节点
return pre # 返回头指针,头指针就是迭代最后一次的head(赋值给类pre) if __name__ == '__main__': three = Node()
three.value = 3 two = Node()
two.value = 2
two.next = three one = Node()
one.value = 1
one.next = two head = Node()
head.value = 0
head.next = one newhead = reverse_list(head)
while newhead:
print newhead.value
newhead = newhead.next
比较形象的图

2,递归
# 临界点:head.next为None
# 先递归到 把最后一个节点指向 newhead
# 然后一步步从后往前逆置 def reverse_recursion(head):
if not head or not head.next:
return head new_head = reverse_recursion(head.next) head.next.next = head
head.next = None
return new_head
python 链表的更多相关文章
- Python链表的实现与使用(单向链表与双向链表)
参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...
- Python链表操作(实现)
Python链表操作 在Python开发的面试中,我们经常会遇到关于链表操作的问题.链表作为一个非常经典的无序列表结构,也是一个开发工程师必须掌握的数据结构之一.在本文中,我将针对链表本身的数据结构特 ...
- python 链表表达式 map、filter易读版
链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...
- Python链表与反链表
# -*- coding:utf8 -*- #/usr/bin/env python class Node(object): def __init__(self, data, pnext = None ...
- python链表的实现
根据Problem Solving with Algorithms and Data Structures using Python 一书用python实现链表 书籍在线网址http://intera ...
- python链表的实现,有注释
class Node(): #node实现,每个node分为两部分:一部分含有链表元素,成数据域;另一部分为指针,指向下一个 __slots__=['_item' ...
- python 链表的反转
code #!/usr/bin/python # -*- coding: utf- -*- class ListNode: def __init__(self,x): self.val=x self. ...
- python 链表、堆、栈
简介 很多开发在开发中并没有过多的关注数据结构,当然我也是,因此,我写这篇文章就是想要带大家了解一下这些分别是什么东西. 链表 概念:数据随机存储,并且通过指针表示数据之间的逻辑关系的存储结构. 链表 ...
- Add Two Numbers(from leetcode python 链表)
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
随机推荐
- Asp.Net网站统一处理错误信息
1.创建Global.asax文件 2.在Application_Error里统一处理,可以写入文件,也可以写入SQL.代码如下 Exception ex = Server.GetLastError( ...
- UDP标准模型
伪代码 #服务端 #创建UDP服务器 ss = socket() #创建一个服务器套接字 ss.bind() #绑定服务器套接字 inf_loop: #服务器无限循环 cs = ss.recvfrom ...
- 在vi或vim上查找字符串
从开头搜索 在命令模式下,输入/你要查找的字符 按下回车,可以看到vim把光标移动到该字符处 再按n(小写)查看下一个匹配 按N(大写)查看上一个匹配, capslock切换大小写,也可以在小写状态下 ...
- 20145229吴姗珊《java程序设计》第2次实验报告
20145229吴姗珊<java程序设计>第2次实验报告 实验名称 Java面向程序设计,采用TDD的方式设计有关实现复数类Complex. 理解并掌握面向对象三要素:封装.继承.多态. ...
- 为UniDBEdit添加拖拽属性
不知是作者Fashard的疏忽还是有意,UniDBEdit的拖拽属性居然没有发布出来(其他组件都已发布).加上其实也很简单. 打开source目录下的uniDBEdit.pas单元,在TUniDBEd ...
- CSS缎带效果
1. [代码]ribbon.html <!DOCTYPE HTML><html><head><style type="text/css&qu ...
- SmartGit(试用期30后),个人继续使用的方法。
在我们做项目的过程中,我们会用到SmartGit这个软件来将本地的MAVEN项目push到国内的码云(https://git.oschina.net)或者是国外的github网站进行项目的管理,这个时 ...
- SpringBoot_异常_01_Caused by: java.lang.BootstrapMethodError: java.lang.NoSuchMethodError
一.异常信息 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonB ...
- 使用Chrome采集摄像头并生成视频下载
主要使用2个技术点:WebRtc 的 getUserMedia 和 MediaRecorder 注意点 开始录制调用 start 方法要传入一个采样间隔,这样录制的媒体会按照你设置的值进行分割成一个个 ...
- MFC实现COM组件
一般而言,ATL实现了对COM组件最好的支持,所以不用MFC实现COM组件.但是MFC实际上也是可以实现COM组件的. 一.MFC DLL优点: MFC com组件可以将MFC的类型作为参数进行传递, ...