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.

题目要求:

求两个链表的交点,如果没有,则返回NULL

要求O(n)的时间复杂度和O(1)的空间复杂度

解题思路:

1、如果不考虑空间复杂度,可以用set容器记录第一个链表的所有结点,依次遍历第二个链表,第一个存在set中的结点即为交点,否则不存在。

2、第一个链表长为x,第二个链表长为y,假设x>y,让第一个链表指针先走x-y步,(这样两个链表指针就长度对齐),然后两个链表指针一起走,如果遇到对应相等,则为交点,否则不存在。

代码:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int getLength(ListNode *head){
int i=;
for(ListNode *p=head;p!=NULL;p=p->next)
i++;
return i;
} ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA==NULL || headB==NULL) return NULL;
int lenA=,lenB=;
ListNode *pA,*pB;
pA=headA;
pB=headB; lenA=getLength(pA);
lenB=getLength(pB); if(lenA>lenB){
for(int i=lenA-lenB;i>;i--)
pA=pA->next;
} if(lenB>lenA){
for(int i=lenB-lenA;i>;i--)
pB=pB->next;
} while(pA!=pB){
pA=pA->next;
pB=pB->next;
} if(pA==pB)
return pA;
else
return NULL;
}
};

(LeetCode 160)Intersection of Two Linked Lists的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. LeetCode算法题-Intersection of Two Linked Lists(Java实现)

    这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...

  4. LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. (LinkedList)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. (LeetCode 83)Remove Duplicates from Sorted Lists

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  7. [LeetCode 题解]:Intersection of Two Linked Lists

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Suppose an ...

  8. LeetCode题解之Intersection of Two Linked Lists

    1.题目描述 2.问题分析 使用unordered_set 将链表A中的节点地址全部插入,然后使用链表B中的每个节点在A中查找. 3.代码 ListNode *getIntersectionNode( ...

  9. [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 ...

随机推荐

  1. iptables配置允许mysql远程访问

    vi /etc/sysconfig/iptables iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCE ...

  2. bzoj2243 染色

    Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如 ...

  3. hdu 1150 Machine Schedule 最少点覆盖

    Machine Schedule Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  4. StringUtils类中 isEmpty() 与 isBlank()的区别

    org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String st ...

  5. python知识(3)----正则表达式

    python的正则表达式使用起来非常的方便,基本思路就是编译规则,匹配字符串,输出字符串 参考资料 Python中的正则表达式教程

  6. SYSCALL_DEFINE3

    http://blog.csdn.net/yueyingshaqiu01/article/details/48786961

  7. Solaris10 下mysql5.5.12的安装

    http://blog.csdn.net/ocean20/article/details/6417845 http://howtolamp.com/lamp/mysql/5.6/installing/

  8. 读书笔记-APUE第三版-(8)进程控制

    进程ID 每一个进程都有一个唯一的进程ID.几个特殊进程: 0号进程是内核进程,一般是调度进程swapper. 1号进程init,是用户进程(以root权限执行/sbin/init),负责初始化. 几 ...

  9. jQuery 二级菜单,一次显示一个小类 鼠标点击显示小类

    jQuery 二级菜单,一次显示一个小类 鼠标点击显示小类 本例有另外2个关联案例,演示地址分别为2.php,3.php 演示 XML/HTML Code <div class="ar ...

  10. Pig系统分析(5)-从Logical Plan到Physical Plan

    Physical Plan生成过程 优化后的逻辑运行计划被LogToPhyTranslationVisitor处理,生成物理运行计划. 这是一个经典的Vistor设计模式应用场景. 当中,LogToP ...