C++实现LeetCode(160.求两个链表的交点)
[leetcode] 160.intersection of two linked lists 求两个链表的交点
write a program to find the node at which the intersection of two singly linked lists begins.
for example, the following two linked lists:
a: a1 → a2
↘
c1 → c2 → c3
↗
b: b1 → b2 → b3
begin to intersect at node c1.
notes:
- if the two linked lists have no intersection at all, return null.
- the linked lists must retain their original structure after the function returns.
- you may assume there are no cycles anywhere in the entire linked structure.
- your code should preferably run in o(n) time and use only o(1) memory.
credits:
special thanks to for adding this problem and creating all test cases.
我还以为以后在不能免费做oj的题了呢,想不到 oj 又放出了不需要买书就能做的题,业界良心啊,哈哈^_^。这道求两个链表的交点题要求执行时间为 o(n),则不能利用类似冒泡法原理去暴力查找相同点,事实证明如果链表很长的话,那样的方法效率很低。我也想到会不会是像之前删除重复元素的题一样需要用两个指针来遍历,可是想了好久也没想出来怎么弄。无奈上网搜大神们的解法,发觉其实解法很简单,因为如果两个链长度相同的话,那么对应的一个个比下去就能找到,所以只需要把长链表变短即可。具体算法为:分别遍历两个链表,得到分别对应的长度。然后求长度的差值,把较长的那个链表向后移动这个差值的个数,然后一一比较即可。代码如下:
c++ 解法一:
class solution { public: listnode *getintersectionnode(listnode *heada, listnode *headb) { if (!heada || !headb) return null; int lena = getlength(heada), lenb = getlength(headb); if (lena < lenb) { for (int i = 0; i < lenb - lena; ++i) headb = headb->next; } else { for (int i = 0; i < lena - lenb; ++i) heada = heada->next; } while (heada && headb && heada != headb) { heada = heada->next; headb = headb->next; } return (heada && headb) ? heada : null; } int getlength(listnode* head) { int cnt = 0; while (head) { ++cnt; head = head->next; } return cnt; } };
java 解法一:
public class solution { public listnode getintersectionnode(listnode heada, listnode headb) { if (heada == null || headb == null) return null; int lena = getlength(heada), lenb = getlength(headb); if (lena > lenb) { for (int i = 0; i < lena - lenb; ++i) heada = heada.next; } else { for (int i = 0; i < lenb - lena; ++i) headb = headb.next; } while (heada != null && headb != null && heada != headb) { heada = heada.next; headb = headb.next; } return (heada != null && headb != null) ? heada : null; } public int getlength(listnode head) { int cnt = 0; while (head != null) { ++cnt; head = head.next; } return cnt; } }
这道题还有一种特别巧妙的方法,虽然题目中强调了链表中不存在环,但是我们可以用环的思想来做,我们让两条链表分别从各自的开头开始往后遍历,当其中一条遍历到末尾时,我们跳到另一个条链表的开头继续遍历。两个指针最终会相等,而且只有两种情况,一种情况是在交点处相遇,另一种情况是在各自的末尾的空节点处相等。为什么一定会相等呢,因为两个指针走过的路程相同,是两个链表的长度之和,所以一定会相等。这个思路真的很巧妙,而且更重要的是代码写起来特别的简洁,参见代码如下:
c++ 解法二:
class solution { public: listnode *getintersectionnode(listnode *heada, listnode *headb) { if (!heada || !headb) return null; listnode *a = heada, *b = headb; while (a != b) { a = a ? a->next : headb; b = b ? b->next : heada; } return a; } };
java 解法二:
public class solution { public listnode getintersectionnode(listnode heada, listnode headb) { if (heada == null || headb == null) return null; listnode a = heada, b = headb; while (a != b) { a = (a != null) ? a.next : headb; b = (b != null) ? b.next : heada; } return a; } }
类似题目:
minimum index sum of two lists
参考资料:
到此这篇关于c++实现leetcode(160.求两个链表的交点)的文章就介绍到这了,更多相关c++实现求两个链表的交点内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 浅谈TypeScript 索引签名的理解