[LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins.
Notice
- 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.
The following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
Your code should preferably run in O(n) time and use only O(1) memory.
LeetCode上的原题,请参见我之前的博客Intersection of Two Linked Lists。
解法一:
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return NULL;
int lenA = getLength(headA), lenB = getLength(headB);
if (lenA < lenB) {
for (int i = ; i < lenB - lenA; ++i) headB = headB->next;
} else {
for (int i = ; i < lenA - lenB; ++i) headA = headA->next;
}
while (headA && headB && headA->val != headB->val) {
headA = headA->next;
headB = headB->next;
}
return (headA && headB) ? headA : NULL;
}
int getLength(ListNode* head) {
int cnt = ;
while (head) {
++cnt;
head = head->next;
}
return cnt;
}
};
解法二:
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
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;
}
};
[LintCode] Intersection of Two Linked Lists 求两个链表的交点的更多相关文章
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [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 ex ...
- 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 ex ...
- ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Intersection of Two Linked Lists (求两个单链表的相交结点)
题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Intersection of Two Linked Lists(两个链表的第一个公共节点)
来源:https://leetcode.com/problems/intersection-of-two-linked-lists Write a program to find the node a ...
- (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 ex ...
- [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点
方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ...
随机推荐
- JNDI
这两天研究了一下 context.lookup("java:comp/env/XXX")和直接context.lookup("XXX")的区别 网上关于这两个的 ...
- ReWriteDateControll
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- OutofMemory之PermGen介绍
可参考:https://my.oschina.net/benhaile/blog/214159.
- 深入理解 spring 容器,源码分析加载过程
Spring框架提供了构建Web应用程序的全功能MVC模块,叫Spring MVC,通过Spring Core+Spring MVC即可搭建一套稳定的Java Web项目.本文通过Spring MVC ...
- postgres索引创建、 存储过程的创建以及在c#中的调用
postgres创建索引参考 http://www.cnblogs.com/stephen-liu74/archive/2012/05/09/2298182.html CREATE TABLE tes ...
- Android 自定义ToolBar详细使用
自定义xml设置ToolBar,通过menu文件扩展选项,通过继承baseactivity使用 1.ToolBar布局 <?xml version="1.0" encodin ...
- design包 TabLayout使用
类似"网易新闻"UI设计就很好,顶部是导航,下面是各个页面.如图 这种效果使用design包中的TabLayout可以轻松的实现. 一.分析TabLayout 常见 UI 上图 ...
- Maven创建工程 WEB
http://www.zuidaima1.com/blog/1618180875144192.htm http://www.zuidaima1.com/blog/1618162161323008.ht ...
- Flume(4)实用环境搭建:source(spooldir)+channel(file)+sink(hdfs)方式
一.概述: 在实际的生产环境中,一般都会遇到将web服务器比如tomcat.Apache等中产生的日志倒入到HDFS中供分析使用的需求.这里的配置方式就是实现上述需求. 二.配置文件: #agent1 ...
- Python-Hello world!
一.Python安装 Windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python3.5 3.配置环境变量: [右键计 ...