Leetcode 160 Intersection of Two Linked Lists 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null.
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
我的方法是:
(1)统计两个链表的长度
(2)移动较长的链表的表头,使得让两个链表的长度一致
(3)从修正后的表头出发对比两个链表的节点是否一致,输出一致的节点,否则输出null
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int list_len(ListNode* head){
int cnt = ;
for(ListNode* now = head; now; now = now->next){
++cnt;
}
return cnt;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int al = list_len(headA);
int bl = list_len(headB);//(1)
ListNode* minh = headA;
ListNode* maxh = headB;
int cnt = bl - al;
if(al > bl) {
maxh = headA;
minh = headB;
cnt = -cnt;
}
while(cnt--){
maxh = maxh->next;
} //(2)
for( ;maxh && minh ; maxh = maxh->next, minh = minh->next){
if(maxh == minh) return maxh;
}
return NULL; //(3)
}
};
Leetcode 160 Intersection of Two Linked Lists 单向链表的更多相关文章
- [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(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 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
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Java for 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 ...
- Java [Leetcode 160]Intersection of Two Linked Lists
题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- (链表 双指针) 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 ...
随机推荐
- MySQL中如何插入反斜杠,反斜杠被吃掉,反斜杠转义
问题描述:mysql中带有反斜杠的内容入库后,发现反斜杠无故失踪了(俗话说被吃掉了) 例:插入insert into tb('url') values('absc\eeee'); 结果数据库里的内容是 ...
- Floyd | | jzoj[1218] | | [Usaco2009 Dec]Toll 过路费 | | BZOJ 1774 | | 我也不知道该怎么写
写在前面:老师说这一道题是神题,事实上确实如此,主要是考察对Floyd的理解 ******************************题目.txt************************* ...
- JQuery UI Autocomplete与jquery.autocomplete.js
程序中要把一个select改成可以下拉搜索的,就想到了使用下autocomplete.js大概是这么个东西. 问了下同学,推荐我使用Jquery Ui autocomplete,下载下来开始调试使用, ...
- 华为手机打开Logcat的方法
华为手机默认是关闭logcat信息的,这在开发调试时当然很不方便,打开log信息的方法如下 1. 进入拨号界面输入:*#*#2846579#*#* 2. 依次选择ProjectMenu---后台设置 ...
- .net mvc 微信支付
一.微信第三方登录 通过微信打开链接:http://www.hzm.com/Entry/Login 微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server ...
- aix 6+ mount 光驱
AIX 挂载光驱的方法 系统环境: [root@Big A:/1]#oslevel -s6100-06-00-0000 [root@Big A:/]#crfs -v cdrfs -p ro -d '/ ...
- struts2获得请求参数的方式
1.用Action的属性接收参数 2.用DomainModel(域模型)接收参数 3.用ModelDriven接收参数 使用这种方式接受参数需要实现ModelDriven接口,
- HDU2243_考研路茫茫――单词情结
给出一些词根,问你有多少种长度为L的串包含至少一个词根. 去年就在敲这个题了,今年才敲出来,还是内牛满面之中... 要求包含至少一个的情况,需要求出所有的情况,减去一个都没有的情况就可以了. 对于给出 ...
- C++ STL中vector(向量容器)使用简单介绍
原文:http://www.seacha.com/article.php/knowledge/cbase/2013/0903/2205.html C++ vector(向量容器)是一个线性顺序结构.相 ...
- js-DOM-页面元素的兼容性、常用事件、节点
页面元素的兼容性: 所谓的兼容性指的就是当前浏览器是否支持当前对象的属性或是方法,如果支持就是兼容,如果不支持就是不兼容. 举个例子: /** * 设置页面标签之间的文本内容的兼容性写法 * @par ...