python 模拟链表
来源:互联网 发布:http默认端口号 编辑:程序博客网 时间:2024/06/08 19:02
class Node(): def __init__(self,value,next=None): self.value = value self.next = next def CreateList(n): if n<=0: return False if n == 1: return Node(1) else: root = Node(1) tmp = root for i in range(2,n+1): tmp.next = Node(i) tmp = tmp.next return rootdef printList(head): p = head while p!=None: print p.value p = p.nextdef listLen(head): cnt = 0 p = head while(p!=None): p = p.next cnt += 1 return cntdef insert(head,n): if n<1 or n>listLen(head): return p = head for i in range(1,n-1): p = p.next a = raw_input("Enter a value:") t = Node(value = a) t.next = p .next p.next = t return head def dellist(head,n): if n<1 or n>listLen(head): return head elif n is 1: head = head.next else: p = head for i in range(1,n-1): p = p.next q = p.next p.next = q.next return headdef mergeTwoLists(l1, l2): if l1 is None: return l2 if l2 is None: return l1 pHead = Node(0) if l1.value <l2.value: pHead = l1 pHead.next = mergeTwoLists(l1.next,l2) else: pHead = l2 pHead.next = mergeTwoLists(l1,l2.next) return pHead if __name__ == "__main__": print "Create a linklist" head1 = CreateList(7) head2 = CreateList(8) head = mergeTwoLists(head1,head2) printList(head)
阅读全文
0 0
- Python 模拟链表
- python 模拟链表
- Python模拟线程并发
- python脚本模拟浏览器
- python模拟静态类
- python实现模拟登录
- Python 模拟键盘输入
- Python 模拟键盘输入
- python实现模拟登录
- python实现模拟登录
- Python 模拟鼠标操作
- 用Python模拟键盘输入
- python模拟Django框架
- python实现模拟登录
- Python 模拟Ajax/XMLHttpRequest
- Python模拟键盘输入
- python 模拟百度搜索关键词
- python实现模拟登录
- 弱鸡,C# System.Timers.Timer测试
- CodeForces 431# div.2 A Odds and Ends 暴力 贪心
- virtual box虚拟机安装+ubuntu14.04系统安装+GCC4.8编译器安装
- 城市正视图,紫书P132UVa221
- HDU —2012 素数判定
- python 模拟链表
- 简单的分割法
- javax.imageio.IIOException: Can't create output stream解决方法
- IDEA下载安装/破解/配置
- mapper.xml是怎样实现Dao层接口
- shell中单引号,双引号以及反引号的区别
- es6语法——解构
- C/C++ 中的0长数组(柔性数组)
- 运算符