[Linked List]Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
- A: a1 → a2
- ↘
- c1 → c2 → c3
- ↗
- B: b1 → b2 → b3
begin to intersect at node c1.
Notes:
- If the two linked lists have no intersection at all, return
null
. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode(int x) : val(x), next(NULL) {}
- * };
- */
- class Solution {
- public:
- int getListLength(ListNode* head){
- int len = ;
- while(head){
- len++;
- head=head->next;
- }
- return len;
- }
- ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
- int lenA = getListLength(headA);
- int lenB = getListLength(headB);
- ListNode* common = NULL,*startA=headA,*startB=headB;
- if(lenA<lenB){
- int diff = lenB-lenA;
- while(diff--) startB=startB->next;
- }else{
- int diff = lenA-lenB;
- while(diff--) startA=startA->next;
- }
- while(startA){
- if(startA == startB){
- common = startA;
- break;
- }
- startA=startA->next;
- startB=startB->next;
- }
- return common;
- }
- };
[Linked List]Intersection of Two Linked Lists的更多相关文章
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- LeetCode: Intersection of Two Linked Lists 解题报告
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(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- LeetCode_160. Intersection of Two Linked Lists
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [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 ...
随机推荐
- 【SQL】大杂烩
--------------------------------- 索引 --------------------------------- 语法: CREATE [索引类型] INDEX 索引名称 ...
- UMl概述(转)
1. UML的组成 UML由视图(View).图(Diagram).模型元素(Model Element)和通用机制(General Mechanism)等几个部分组成. a) 视图(View): 是 ...
- .Net类型与JSON的映射关系
首先谢谢大家的支持和关注.本章主要介绍.Net类型与JSON是如何映射的.我们知道JSON中类型基本上有三种:值类型,数组和对象.而.Net中的类型比较多.到底它们是如何映射的呢? 总体来讲,Json ...
- Spring-----7、bean实例的创建方式及依赖配置
转载自:http://blog.csdn.net/hekewangzi/article/details/45648579
- CAA调试
在需要调试的Module(*.m)上右键,选择属性,命令位置选择你的framework目录 路径选择对应工程目录下的\intel_a(或者Win64 -- 64位机器) 然后就可以尽 ...
- Dijkstra算法(迪杰斯塔拉算法)
算法描述: Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法能得出最 ...
- AngularJS 不得不了解的服务 $compile 用于动态显示html内容
项目中一度纠结与AngularJS如何动态显示不同的html内容. 本来是希望直接使用下面的语句来实现: <div> </div> 但是很尴尬的是,这样不能识别出html标签, ...
- c#局域网聊天软件的实现
本软件是基于大学寝室局域网聊天的思路.c#源代码如下: using System; using System.Drawing; using System.Collections; using Syst ...
- Thinking in Java——集合(Collection)
一.ArrayList的使用(略) 二.容器的基本概念 (一).Collection是集合类的基本接口 主要方法: public interface Collection<E>{ bool ...
- filestream streamreader
filestream是一个读取文件的stream,其本身也是支持read和write的,负责的对文件的读与写,而streamreader则是建立在对流的基础上的读,同时还有streamwrite ht ...