LL160IntersectionOfTwoLinkedLists

思路

  • use two nested loops
  • Use reverse list
    • Met a problem. We have to rebuilt two new lists to reverse them, but the ListNode cannot be the ones in original lists.
  • Virtual Cycles
    • However, if no intersection, the while loop will be endless
  • Till then, I notice that the intersection part will always be the latter part towards the tail of both lists, so we can check the ( len - k) th node to see if that’s the beginning of the intersection
    • But, as the random length before the intersection part, it is hard to say how long will move one-by-one for the other list
    • Till now, I missed somthing, Actually there is one more property we can notice, is that The intersection part will be covered by both lists. We can start to check from the node that both lists have the same number of remaining nodes!
  • Correct thinking https://discuss.leetcode.com/topic/33291/recommend-for-beginners-clean-c-implementation-with-detailed-explaination
    • From the above solution, Knowing the length of each list is very complicated.
    • But our goal is to make two pointers pA and pB to stop at the node that the number of remaining nodes == length of shorter list
    • We can traverse both list, suppose listA.length < listB.length. After pA reaches the end of listA, pA will go to headB. So that when pB traversed to the end, pA stopped at the node that remaining number of notes are same as length of listA.
    • Finish this by one method. A lot of bugs recorded in comments
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值