题目

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

For example, the following two linked lists:



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.

分析

给定两个链表,求它们的交叉节点。

要求,时间复杂度在O(n)内,空间复杂度为O(1)

两个链表的长度不定,但是交叉节点的后续节点全部相同,所以先求得每个链表的长度lenA和lenB,将较长的链表先移动|lenA−lenB|个位置,然后同时后移,遇到的第一个值相等的节点既是要求的交叉节点。

AC代码

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB)
return NULL; ListNode *p = headA, *q = headB; //求出输入两个链表的长度
int lenA = 0, lenB = 0;
while (p)
{
++lenA;
p = p->next;
}//while while (q)
{
++lenB;
q = q->next;
}//while //让长的链表先移动多出的节点
p = headA;
q = headB;
if (lenA > lenB)
{
int i = 0;
while (p && i < lenA - lenB)
{
p = p->next;
++i;
}//while
}
else{
int j = 0;
while (q && j < lenB - lenA)
{
q = q->next;
++j;
}//while
} while (p && q && p->val != q->val)
{
p = p->next;
q = q->next;
}//while return p;
}
};

GitHub测试程序源码

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

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

  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之“链表”:Intersection of Two Linked Lists

    此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...

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

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

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

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

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. 线程池(2)Executors.newFixedThreadPool

    例子: ExecutorService es = Executors.newFixedThreadPool(5); try { for (int i = 0; i < 20; i++) { Ru ...

  2. linux替换文件中的某个字符串的命令sed

    sed -i 's/java-7-oracle/java-8-oracle/g' /etc/init.d/tomcat7 上面的命令是将tomcat7中的java-7-oracle替换为java-8- ...

  3. setTimeout() 实现程序每隔一段时间自动执行

    定义和用法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 参数 描述 code 必需.要调用的函数后要执行的 Ja ...

  4. 次小生成树(SST)

    次小生成树(SST) 题目背景 Awson是某国际学校信竞组的一只菜鸡.Awson最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当Awson洋洋得意之时,信竞组其他大 ...

  5. ruby YAML.load 和YAML.load_file区别

    1. load( io ) Load a document from the current io stream. File.open( 'animals.yaml' ) { |yf| YAML::l ...

  6. 洛谷P3928 SAC E#1 - 一道简单题 Sequence2

    提交地址 题目背景 小强和阿米巴是好朋友. 题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强. ...

  7. zk实现服务选举

    非公平选举算法1)首先通过zk创建一个 /server 的PERSISTENT节点 2)多台机器同时创建 /server/leader EPHEMERAL子节点 3)子节点只能创建一个,后创建的会失败 ...

  8. @PropertySource

    功能 加载指定的属性文件(*.properties)到 Spring 的 Environment 中.可以配合 @Value 和 @ConfigurationProperties 使用. @Prope ...

  9. java http的get,post请求

    初学可用F12查看任意网页帮助理解 package httpTest: import java.io.BufferedReader; import java.io.IOException;import ...

  10. CF1157D N Problems During K Days

    思路: 在1, 2, 3, ... , k的基础上贪心构造. 实现: #include <bits/stdc++.h> using namespace std; typedef long ...