CC150 Reading Notes 2: Linked Lists

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值