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 ...
随机推荐
- java io学习之File类
1.先看下四个静态变量 static String pathSeparator The system-dependent path-separator character, represented a ...
- 自动布局之autoresizingMask
UIViewAutoresizing是一个枚举类型,默认是UIViewAutoresizingNone,也就是不做任何处理. 1 2 3 4 5 6 7 8 9 typedef NS_OPTIONS( ...
- 【转】ASP.NET ViewState详解
(wyt今天学习了这篇文章,作为门外汉的我了解了很多页面控件数据加载的知识和viewstate的用法和原理.我想在日后的开发效率提升上会有很大的作用.) 转自http://www.cnblogs.co ...
- mysql术语解释
数据库(database): 数据表的集合: 数据表 (table):数据的矩阵: 列(column): 相同属性的数据的集合: 行(row): 一个对象的各种属性数据的集合: 冗余():一个字段在多 ...
- 安装window服务
1 使用管理员权限启动命令提示符 2 输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 3 输入installUtil.exe 服务文件所在目录 ...
- Servlet练习
编写一个Servlet,注册登录成功后,讲表单中的内容输出到页面当中 <%@ page language="java" contentType="text/html ...
- Android瀑布流照片墙实现,体验不规则排列的美感
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10470797 传统界面的布局方式总是行列分明.坐落有序的,这种布局已是司空见惯,在 ...
- 传递引用类型参数(ref)
引用类型的变量不直接包含其数据:它包含的是对其数据的引用. 当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值. 但是无法更改引用本身的值:也就是说,不能使用相同的引用为新类分配 ...
- LINQ to SQL大全
LINQ to SQL语句 (1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的 ...
- 【概念笔记】JavaEE - web part2
IT`huhui前言录 续JavaEE - web part1 链接http://www.cnblogs.com/ithuhui/p/5930745.html, 持续修改更新. Cookie 1. 定 ...