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.

找出第一个合并的节点的位置(或者说插入),注意复杂度为O(N),那么先遍历两个链表,求出长度,把长的那个链表前面的“剪去”,下面的就好比较了,代码如下:

 class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int len1, len2;
len1 = len2 = ;
ListNode * p = headA;
ListNode * q = headB;
while(p){
p = p->next;
len1++;
}
while(q){
q = q->next;
len2++;
}
if(len1 > len2){
int diff = len1 - len2;
while(diff--)
headA = headA->next;
}else if(len2 > len1){
int diff = len2 - len1;
while(diff--)
headB = headB->next;
}
while(headA){
if(headA == headB)
return headA;
headA = headA->next;
headB = headB->next;
}
return NULL;
}
};

LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)的更多相关文章

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

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

  3. intersection of two linked lists.(两个链表交叉的地方)

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

  4. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  5. [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点

    方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ...

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

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

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

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

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

随机推荐

  1. web前端编码规范

    简要介绍 本文通过参考百度腾讯等前端编码规范(链接建文末),得出个人习惯的编码规范.个人编码规范采用在不影响可读性的情况下能省就省,尽量简洁,不需要就直接去掉. 最佳原则不管是个人编码规范还是团队编码 ...

  2. 测试Windows live Writer

    private String GetRandomint(int codeCount) { Random random = new Random(); string min = "" ...

  3. NIO服务端和客户端通信demo

    代码转自 https://www.jianshu.com/p/a9d030fec081 服务端: package nio; import java.io.IOException; import jav ...

  4. C++模板专门化与重载

    最近在复习C++有关知识,又重新看<<Effective C++>>,收获颇丰.原来以前看这边书,好多地方都是浅尝辄止.<<Effective C++>> ...

  5. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  6. [转]HBase hbck——检察HBase集群的一致性

    Hbase提供了hbck命令来检查各种不一致问题.hbck的名字仿效了HDFS的fsck命令,后者是一个用于检查HDFS中不一致问题的工具.下面这段非常易懂的介绍出自于hbck的源程序. 检查数据在M ...

  7. JavaScript返回顶部特效

    样式: <style type="text/css"> /*返回顶部特效*/ a { border: none; text-decoration: none; outl ...

  8. Resharper 快捷键

    编辑   Ctrl + Space 代码完成 Ctrl + Shift + Space代码完成 Ctrl + Alt + Space代码完成 Ctrl + P 显示参数信息 Alt + Insert ...

  9. 存储库之mongodb,redis,mysql

    一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性 ...

  10. CSS3鼠标悬停8种动画特效

    在线演示 本地下载