Chapter 2 Linked Lists - 2.2
来源:互联网 发布:女神联盟坐骑升阶数据 编辑:程序博客网 时间:2024/05/18 17:03
Problem 2.2: Implement an algorithm to find the nth to last element of a singly linked list.
The solution on answer page needs only one iteration while other intuitive solution takes more than one iteration.
The solution on answer page needs only one iteration while other intuitive solution takes more than one iteration.
def find_nth_to_last(head, n): p1 = p2 = head # Move p2 n nodes forward for i in range(0, n): # If p2 runs into the end of list, # the input is invalid if p2 == None: return False p2 = p2.next # Move p2 and p1 together until # p2 runs into the end of list while True: if p2 == None: return p1 p2 = p2.next p1 = p1.nextI learned that sometimes we can use more than one pointers to achieve a better solution.
- Chapter 2 Linked Lists - 2.2
- Chapter 2 Linked Lists - 2.1
- Chapter 2 Linked Lists - 2.3
- Chapter 2 Linked Lists - 2.4
- Chapter 2 Linked Lists - 2.5
- Chapter 2 | Linked Lists Cracking
- CareerCup chapter 2 Linked Lists
- 【算法】【CareerCup】Chapter 2----Linked Lists
- Chapter 2 | Linked Lists--实现两个单链表数据的和
- Chapter 2 | Linked Lists--移除未排序链表中的重复项
- Chapter 2 | Linked Lists--查找循环链表中的环的开始结点
- chapter 2 Linked List
- Chapter 2 | Linked Lists--返回单链表倒数第n个元素及删除中间的某个节点
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- 【CareerCup】 Linked Lists—Q2.2
- CC150 Reading Notes 2: Linked Lists
- Linked Lists Part1-Singly Linked Lists
- Add two numbers represented by linked lists | Set 2
- mdadm软raid1恢复过程
- 再谈存储-IOPS 计算方法
- VC中Radio控件的用法
- jQuery validate验证隐藏表单(hidden)域
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
- Chapter 2 Linked Lists - 2.2
- 经验笔记:windows疑难杂症
- xstream:实现JavaBean与XML/JSON的相互转换
- JSP内置对象----request
- Chapter 2 Linked Lists - 2.3
- Jboss启动机制
- operator new与new operator 的区别
- Chapter 2 Linked Lists - 2.4
- 用lua获取目录,文件名,扩展名