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 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;
}
};
LeetCode (160) Intersection of Two Linked Lists的更多相关文章
- (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 ...
- (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 ...
- 【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 ...
- LeetCode之“链表”:Intersection of Two Linked Lists
此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...
- [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 ...
- [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 ...
- 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 ...
- 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(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
随机推荐
- LinkedList源码及原理
简介 内部结构分析 LinkedList源码分析 构造方法 添加(add)方法 根据位置取数据的方法 根据对象得到索引的方法 检查链表是否包含某对象的方法: 删除(remove/pop)方法 Link ...
- 字节码技术---------动态代理,lombok插件底层原理。类加载器
字节码技术应用场景 AOP技术.Lombok去除重复代码插件.动态修改class文件等 字节技术优势 Java字节码增强指的是在Java字节码生成之后,对其进行修改,增强其功能,这种方式相当于对应用 ...
- Redis特性之持久化机制
持久化机制 Redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到硬盘来保证持久化. Redis支持两种持久化方式: 1.snapshotting(快照)也是默认方式 ...
- CSS属性、伪类选择器,CSS3选择器
CSS1时IE6是部分支持,伟大的IE6!CSS2时IE6部分支持,伟大的IE6依旧是部分支持!CCS3盛行CSS4也已经提上日程的现在,IE6完全不支持.IE6你该走了,我们会永远记住你的功绩的!I ...
- 实例——省市区三级联动 & 还可以输入字符统计
1 省市区三级联动 html代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- 常用的 HTML 头部标签
曾几何时,我们已经不再手写 HTML 标签.Emmet.Markdown 等工具让我们「健步如飞」,但是我们真的了解这些标签了吗? 基本标签 使用 HTML5 doctype,不区分大小写. < ...
- If you can't take it, don't dish it out.
If you can't take it, don't dish it out.己所不欲,勿施于人.
- SqlServer中生成一串连续数字
在SQLServer中一串连续数字,如1,2,3,4,5,....或者 1 2 3 4 5 没有现成方法,网上都用通用表表达式递归生成.今天想到一个还算简单的方法,记录下来: select row_n ...
- 2013年省市区/县数据SQL Server(SQL语句)
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[tbl_Region]( [ ...
- jsp另外五大内置对象之response-操作重定向
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...