[LeetCode 题解]:Intersection of Two Linked Lists
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
2. 题意
寻找两个链表的交点。
提示:
1. 如果两个链表没有交集,那么返回NULL。
2. 链表的结果不能发生变化。
3. 两个链表都没有环。
4. 请给出O(n)时间复杂度、O(1)空间复杂度的解法。
3. 思路
分别统计链表A和链表B的长度。
如果两个链表有交集,那么从交点CP开始,后续的所有节点都应该相同。如图所示C1->C2->C3.
两个链表不同的节点分别为a1,a2; b1,b2,b3;
比较两个链表的长度la,lb;
假设la>lb,那么链表A先遍历la-lb节点。从la-lb节点开始,两个链表的长度就相同了。然后依次比较各自节点是否相同,直到找到交点或者到达链表尾部。
4: 解法
- class Solution {
- public:
- int getListLen(ListNode *head){
- int len=0;
- ListNode *root=head;
- while(root){
- root=root->next;
- len++;
- }
- return len;
- }
- ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
- int alen=getListLen(headA),blen=getListLen(headB);
- ListNode *la=headA,*lb=headB;
- if(alen>blen){
- while(alen!=blen){
- la=la->next;
- alen--;
- }
- }
- if(alen<blen){
- while(alen!=blen){
- lb=lb->next;
- blen--;
- }
- }
- while(la!=lb){
- la=la->next;
- lb=lb->next;
- }
- if(!la||!lb){
- return NULL;
- }else{
- return la;
- }
- }
- };
[LeetCode 题解]: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】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 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][003] 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 求两个链表的起始重复位置 --------- java
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 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 ...
随机推荐
- HTTP之Tcp/Ip协议的工作原理
计算机与网络设备要相互通信,双方就必须基于相同的方法.比如,如何探测到通信目标.由哪一边先发起通信.使用哪种语言进行通信.怎样结束通信等规则都需要事先确定.不同的硬件.操作系统之间的通信,所有的这一切 ...
- Form表单标签的Enctype属性的作用及应用示例介绍
Enctype :指定将数据回发到服务器时浏览器使用的编码类型.用于表单里有图片上传. 编码类型有以下三种: application/x-www-form-urlencoded: 在发送前编码所有字符 ...
- 【312】◀▶ arcpy 常用函数说明
其他常用的 ArcPy 函数说明 序号 类名称 功能说明 语法 & 举例 01 RefreshActiveView ====<<<< Description ...
- sqlserver,oracle,mysql等的driver驱动,url怎么写
oracle driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521 ...
- ORA-00604: 递归 SQL 级别 1 出现错误 ORA-01000: 超出打开游标的最大数
有程序没关闭游标, --打开了哪些游标 select * from v$open_cursor 在open cursor之后一定要注意要close cursor(在store procedure里更应 ...
- Hibernate中Session与本地线程绑定
------------------siwuxie095 Hibernate 中 Session 与本地线程绑定 1.Session 类似于 JDBC 的连接 Connection 2.Session ...
- java读取properties配置文件[转]
网上文章常见的几种读取.properties文件的方式 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInp ...
- Windows 毫秒计时
#include <windows.h> #include <iostream> using namespace std; LARGE_INTEGER MilliSecondT ...
- [Groovy] Groovy API
http://www.soapui.org/about-soapui/soapui-faq.html#1-SoapUI--General-Questions 3.1.1. What is Groovy ...
- Web测试-day
昨天太忙忘了写博客,今天补上: 这两天完成的工作: 我们组选定了博客园和CSDN作为对比,进行Web测试. 胡俊辉--找到了10个网页的bug,并完成了bug记录文档,并且对CSDN和博客园进行功能分 ...