您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: