在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 链表的更多相关文章

  1. Python链表的实现与使用(单向链表与双向链表)

    参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...

  2. Python链表操作(实现)

    Python链表操作 在Python开发的面试中,我们经常会遇到关于链表操作的问题.链表作为一个非常经典的无序列表结构,也是一个开发工程师必须掌握的数据结构之一.在本文中,我将针对链表本身的数据结构特 ...

  3. python 链表表达式 map、filter易读版

    链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...

  4. Python链表与反链表

    # -*- coding:utf8 -*- #/usr/bin/env python class Node(object): def __init__(self, data, pnext = None ...

  5. python链表的实现

    根据Problem Solving with Algorithms and Data Structures using Python 一书用python实现链表 书籍在线网址http://intera ...

  6. python链表的实现,有注释

    class Node():                   #node实现,每个node分为两部分:一部分含有链表元素,成数据域;另一部分为指针,指向下一个  __slots__=['_item' ...

  7. python 链表的反转

    code #!/usr/bin/python # -*- coding: utf- -*- class ListNode: def __init__(self,x): self.val=x self. ...

  8. python 链表、堆、栈

    简介 很多开发在开发中并没有过多的关注数据结构,当然我也是,因此,我写这篇文章就是想要带大家了解一下这些分别是什么东西. 链表 概念:数据随机存储,并且通过指针表示数据之间的逻辑关系的存储结构. 链表 ...

  9. Add Two Numbers(from leetcode python 链表)

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

随机推荐

  1. 冒泡排序的三种实现(Java)

    冒泡排序是非常好理解的,以从小到大排序为例,每一轮排序就找出未排序序列中最大值放在最后. 设数组的长度为N: (1)比较前后相邻的二个数据,如果前面数据大于后面的数据,就将这二个数据交换. (2)这样 ...

  2. 简介windows的环境变量

    环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数,比如临时文件夹位置和系统文件夹位置等.这点有点类似于DOS时期的默认路径,当你运行某些程序时除了在当前文件夹中寻找外,还会到设置的默认路 ...

  3. 每天一个Linux命令(17)whereis命令

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息.     (1)用法:   用法:    w ...

  4. [转]eclipse中的常用快捷键

    1.选中你要加注释的区域,用ctrl+shift+C 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上注释3.要修改在eclispe中的命令的快捷键方式我们只需进入windo ...

  5. Luogu-2600 [ZJOI2008]瞭望塔

    把地面看成半平面,能看到所有位置的点所在的区域即为半平面的交 因为分段函数的极值只会在转折处或边界取到,所以对于半平面上和地面上的每一个交点都求一下距离就好了 #include<cmath> ...

  6. 什么是shell【TLCL】

    常用命令 date cal df——report file system disk space usage free——display amount of free and used memory i ...

  7. HTML5学习笔记——1

    1.视频 <vedio src  controls  height width autoplay loop preload > </vedio> <source src ...

  8. 一个ClientDataset的Delta与XML相互转换

    一个ClientDataset的Delta与XML相互转换的文章: 大家都知道TClientDataSet的Delta属性保存数据集的变化,但是Delta是OleVariant类型的属性,这样如果用D ...

  9. Git_学习_06_ 放弃本地修改

    一.未使用 git add 缓存代码时 git checkout -- filepathname // 放弃某个文件 git checkout . // 放弃所有文件 git checkout . 用 ...

  10. C++11 右值引用 与 转移语义

    新特性的目的 右值引用(R-value Reference)是C++新标准(C++11, 11代表2011年)中引入的新特性,它实现了转移语义(Move Semantics)和精确传递(Perfect ...