CC150 Reading Notes 2: Linked Lists
2015-11-09 23:35
302 查看
Notes:
1. implement a linked list
2. notice whether it is a singly linked list or a doubly linked list
3. delete a node: remember to deallocate the removed node
4. "Runner" Technique: two pointers, one is ahead and one is after, the fast pointer might be ahead by fixed amount or hopping multiple nodes each time
5. many linked list problem rely on recursion
Questions:
2.1
Q: remove duplicates from unsorted linked list
A: 1. store every distinct node into a hash_table, when occurring a duplicate, do related operation
2. two pointers: compare current and iteration of all the node after current
2.2
Q: find the kth to last element of a singly linked list
A:
1. iterative: two pointers, one for the current and one is kth ahead of the current
2. recursive: to the end->back and count to k
2.3
Q: Delete a node in a linked list given the access to only this node
A: copy the next node and delete the next node
2.4 Q: Partition a linked list
A: use another two linked list, one for elements smaller than the pivot, the other for the bigger ones
2.5 Q: Numbers are stored in a linked list, implement A plus B
A: 1. number is stored in reverse order----just simulate the process of addition
2. number is stored in forward order----first, pad the shorter list's head with 0s; second, add two lists; third, insert carry if necessary and print out result
2.6 Q: Find the head point of a loop in a linked list(a hard question)
A: 1. FastRunner and SlowRunner to detect a loop
2. when SlowRunner get to the head of loop, FastRunner is at position K, where K = k % LoopSize(K is the the position and k is the loop head position), SlowRunner is LoopSize - K ahead of FastRunner
3. they hit at position LoopSize - K, and head is K ahead of them, make SlowRunner to linked list head and they move together by step 1
4. when SlowRunner forward k steps and k = K + k % LoopSize, so they hit at the head of the loop
2.7 Q: check if a linked list is a palindrome
A: 1. reverse and compare
2. use a stack, SlowRunner into stack, FastRunner reach end
3. recursive method
1. implement a linked list
2. notice whether it is a singly linked list or a doubly linked list
3. delete a node: remember to deallocate the removed node
4. "Runner" Technique: two pointers, one is ahead and one is after, the fast pointer might be ahead by fixed amount or hopping multiple nodes each time
5. many linked list problem rely on recursion
Questions:
2.1
Q: remove duplicates from unsorted linked list
A: 1. store every distinct node into a hash_table, when occurring a duplicate, do related operation
2. two pointers: compare current and iteration of all the node after current
2.2
Q: find the kth to last element of a singly linked list
A:
1. iterative: two pointers, one for the current and one is kth ahead of the current
2. recursive: to the end->back and count to k
2.3
Q: Delete a node in a linked list given the access to only this node
A: copy the next node and delete the next node
2.4 Q: Partition a linked list
A: use another two linked list, one for elements smaller than the pivot, the other for the bigger ones
2.5 Q: Numbers are stored in a linked list, implement A plus B
A: 1. number is stored in reverse order----just simulate the process of addition
2. number is stored in forward order----first, pad the shorter list's head with 0s; second, add two lists; third, insert carry if necessary and print out result
2.6 Q: Find the head point of a loop in a linked list(a hard question)
A: 1. FastRunner and SlowRunner to detect a loop
2. when SlowRunner get to the head of loop, FastRunner is at position K, where K = k % LoopSize(K is the the position and k is the loop head position), SlowRunner is LoopSize - K ahead of FastRunner
3. they hit at position LoopSize - K, and head is K ahead of them, make SlowRunner to linked list head and they move together by step 1
4. when SlowRunner forward k steps and k = K + k % LoopSize, so they hit at the head of the loop
2.7 Q: check if a linked list is a palindrome
A: 1. reverse and compare
2. use a stack, SlowRunner into stack, FastRunner reach end
3. recursive method
相关文章推荐
- Spark修炼之道(高级篇)——Spark源码阅读:第十节 Standalone运行模式解析
- loadView与viewDidLoad不同
- Java记录 -74- 自定义泛型
- 对象包装器与自动(拆箱)装箱+参数数量可变的方法+枚举类
- APNS详解
- KD树——UCI数据集IRIS
- 如何用Java调用R(包含在服务器安装的过程)以及用R生成的图片如何用<img>标签访问
- 深入理解计算机系统(CSAPP)实验二 datalab-handout
- C++ - 异常处理:try,catch,throw,finally的用法
- ToggleButton控件
- 磁盘阵列配置全程解(图)
- [2081]:手机短号
- QuickSort及其变种算法的总结
- Objective-C 中 +load 与 +initialize
- QAction系列详解
- 今天很开心
- Enum Types
- 在linode centos7 上安装 gitlab-ce
- MySQL 的一次错误处理 Got fatal error 1236 from master when reading data from binary log
- UICollectionView实现瀑布流