题目描述:

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.

solution:

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == NULL || headB == NULL)
return NULL;
int lenA = ;
int lenB = ;
ListNode *pA = headA;
while (pA != NULL)
{
++lenA;
pA = pA->next;
}
ListNode *pB = headB;
while (pB != NULL)
{
++lenB;
pB = pB->next;
} if (pA != pB)
return NULL; pA = headA;
pB = headB; if (lenA > lenB)
{
int k = lenA - lenB;
while (k--)
{
pA = pA->next;
}
}
else
{
int k = lenB - lenA;
while (k--)
{
pB = pB->next;
}
} while (pA != pB)
{
pA = pA->next;
pB = pB->next;
}
return pA;
}

  上述解法来自《剑指offer》,还有一种基于Hashset的方法。

solution:

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == NULL || headB == NULL)
return NULL;
unordered_set<ListNode*> st;
while (headA != NULL)
{
st.insert(headA);
headA = headA->next;
} while (headB != NULL)
{
if (st.find(headB) != st.end())
return headB;
headB = headB->next;
}
return NULL;
}

Intersection of Two Linked Lists (求两个单链表的相交结点)的更多相关文章

  1. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

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

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

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

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

  5. 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】

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

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

  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. Intersection of Two Linked Lists(两个链表的第一个公共节点)

    来源:https://leetcode.com/problems/intersection-of-two-linked-lists Write a program to find the node a ...

  9. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

随机推荐

  1. Application.Exit

    Application.Exit:通知winform消息循环退出.Environment.Exit:终止当前进程,返回exitcode给操作系统 Application.Exit会在所有前台线程退出后 ...

  2. BeanShell计算支付价格

    问题:需要获取支付价格,而支付价格是商品价格*折扣 解决:先将商品价格和折扣获取出来,然后使用BeanShell将两个值相乘,获得最后的支付价格 1.使用json提取器提取商品的价格 2.使用正则表达 ...

  3. python高级特性之封包与解包

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:kwsy PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...

  4. 用python从0到1制作动态条形图的过程

    大家好,今天我们要讲的是如何使用Pyecharts制作动态排名变化图

  5. D. Ehab the Xorcist

    题意: 略: 感觉被演了一波,这是CFdiv2吗? 算是这个构造题吧. 1 首先我们可以将u进行二进制拆分来考虑.加入u>v那么小与v的那些数在怎么拼接也无法使异或值为u. 比如二进制U=1 0 ...

  6. B - How many integers can you find 杭电1976

     Now you get a number N, and a M-integers set, you should find out how many integers which are small ...

  7. C语言二维数组超细讲解

    用一维数组处理二维表格,实际是可行的,但是会很复杂,特别是遇到二维表格的输入.处理和输出. 在你绞尽脑汁的时候,二维数组(一维数组的大哥)像电视剧里救美的英雄一样显现在你的面前,初识数组的朋友们还等什 ...

  8. Java中的OOM问题

    OOM是什么 OOM全称"OutOfMemory",既内存溢出.我们知道,Java中的对象是在堆(heap)上创建的,当堆内存不足以为新创建的对象分配空间时,就会产生OutOfMe ...

  9. [De1CTF 2019]SSRF Me

    原帖地址 : https://xz.aliyun.com/t/5927 De1CTF 2019 的一个题目总结 题目描述 直接查看页面源代码可以看到正确格式的代码 #! /usr/bin/env py ...

  10. radio样式

    .radio{ position: relative; border: 1px solid #999; border-radius: 50%; width: 12px; height: 12px; b ...