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 ...
随机推荐
- ajax头像上传
html代码: <input id="fileinput" type="file" /><br /> <br /> < ...
- Selenium2+python自动化24-js处理富文本(带iframe)
前言 上一篇Selenium2+python自动化23-富文本(自动发帖)解决了富文本上iframe问题,其实没什么特别之处,主要是iframe的切换,本篇讲解通过js的方法处理富文本上iframe的 ...
- clang format 官方文档自定义参数介绍(中英文)
官方文档:http://clang.llvm.org/docs/ClangFormatStyleOptions.html 中文 在代码中配置样式 当使用 clang::format::reformat ...
- BIEE 11g 安装
1.安装oracle 创建字符集为AL32UTF8的实力MERIT sys密码merit 创建biee用户 create tablespace biee datafile 'D:\ORADATA\M ...
- 自己随意写了个简单的依赖jquery的轮播图
//轮播图 function Switcher(obj){ this.box = $(obj.box); this.width = this.box.width(); this.banner = $( ...
- mybatis generate 自动生成 entity dao 和 xml 文件
其中的一种方式 ,使用maven 插件 <build> <plugins> <plugin> <groupId>org.mybatis.generato ...
- 购物车界面,不同section,点击增减物品,确定取消选中的逻辑判断
1.首先在自定义的cell中,创建两个代理方法 @protocol shopCartDelegate <NSObject> -(void)shopCartDelegate:(ShopCar ...
- java图形化编程
转载 学习Java Swing图形化编程,我们首先要了解三个最基本的概念:顶层容器,控件,布局. 下面就来介绍一下这三个基本概念 1.顶层容器 什么是顶层容器?当我们使用Java进行图形编程的时候,图 ...
- 5.11-5.15javascript制作动态表格
制作动态表格的主要是运用js中表格类的insertRow.insertCell简易添加行和列的代码,不过要注意每行添加的表格是有位置行编号的,每行的编号为rows.length-1,增加的表格内的标签 ...
- 【转】轮询、长轮询、iframe长连接、web socket
引自:http://www.cnblogs.com/AloneSword/p/3517463.html http://www.cnblogs.com/wei2yi/archive/2011/03/23 ...