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 ...
随机推荐
- Python全栈--9 __import__ 反射和面向对象基础 self 封装 继承(多继承的顺序) 多态
一.反射 python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数分别用于对对象内部执行:检查是否含有某成员.获取成员.设置成员.删 ...
- HTTP协议-引自孤傲苍狼博客
一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...
- java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- angualrjs
$rootScope是angularJS中最接近全局作用域的对象.在$rootScope上附加太多业务逻辑并不是好主意,这与污染JavaScript的全局作用域是一样的. $scope对象就是一个普通 ...
- EasyUI 后台接受DataGrid传来的参数
string ad = Context.Request.QueryString["rows"];不行 string aedf = Context.Request.Form[&quo ...
- 团队作业week8
每个团队编写一个团队项目的功能规格说明书.
- 【11_83】Remove Duplicates from Sorted List
这道题本质上不难,难的是细节处理,容易出错. 第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过. Remove Duplicates from Sorted List My Su ...
- [1001]mod
输入一个数,如果其是3的倍数就输出“3”,如果是2的倍数就输出“2”,都是则输出“1”,否则输出“0”: 输入输出样例: 输入: 9 输出: 3 输入: 7 输出: 0 Hint 使用一下形式的条件语 ...
- 关于ucosII系统的软件系统裁剪性
ucosII是依靠编译时的条件编译来实现软件系统的裁剪性的,即把用户可裁剪的代码段写在#if和#endif预编译指令之间,在编译时根据#if预编译指令后面的常数的值来确定是否该代码段进行编译.在工程文 ...
- C++类库介绍
如果你有一定的C基础可能学起来比较容易些,但是学习C++的过程中又要尽量避免去使用一些C中的思想:平时还要多看一些高手写的代码,遇到问题多多思考,怎样才能把问题抽象化,以使自己头脑中有类的概念:最后别 ...