- 浏览: 183408 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
给定两个链表,判断它们是否交叉,如果没有交叉返回空,如果有交叉,返回交叉的起始点。我们遍历两个链表,判断元素是否相等,但是前提是两个链表长度相同。题目中的两个链表长度不同,我们可以同时遍历两个链表A,B,当A链表为空时让它指向B链表的头结点,当B链表为空时,让B链表指向A链表的头结点(这种情况是A链表比B链表短),同理A链表比B链表长的情况。代码如下:
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.
给定两个链表,判断它们是否交叉,如果没有交叉返回空,如果有交叉,返回交叉的起始点。我们遍历两个链表,判断元素是否相等,但是前提是两个链表长度相同。题目中的两个链表长度不同,我们可以同时遍历两个链表A,B,当A链表为空时让它指向B链表的头结点,当B链表为空时,让B链表指向A链表的头结点(这种情况是A链表比B链表短),同理A链表比B链表长的情况。代码如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode curA = headA; ListNode curB = headB; while(curA != curB) { curA = (curA == null) ? headB : curA.next; curB = (curB == null) ? headA : curB.next; } return curA; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
javascript js_leetcode题解之160-intersection-of-two-linked-lists.js
python python_leetcode题解之160_Intersection_of_Two_Linked_Lists.py
leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。...Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
2. **链表**:链表数据结构在LeetCode中也占据重要地位,如"删除链表中的元素"(Remove Element)和"两链表相交"(Intersection of Two Linked Lists)。Swift虽然没有内置链表,但开发者可以自定义节点结构体来模拟...
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...
例如,“两链表的交点”(Intersection of Two Linked Lists)要求找出两个链表的第一个公共节点。 3. **字符串(Strings)**:处理字符串的拼接、查找、替换等问题。如“无重复字符的最长子串”(Longest ...
3. **链表操作**:如“两链表的交点”(Intersection of Two Linked Lists),需要找到两个链表的第一个公共节点。 4. **树结构**:如“二叉树的最近公共祖先”(Lowest Common Ancestor of a Binary Tree),需要找到...
例如,"两链表的交点"(Intersection of Two Linked Lists)要求找到两个链表的交点,或者"删除链表中的节点"(Remove Nth Node From End of List)需要删除链表的指定位置节点。 3. **字符串**:字符串处理题目...
例如,Reverse Linked List(反转链表)和Intersection of Two Linked Lists(两个链表的交集)等题目,都需要对链表进行操作。 3. 栈与队列:栈具有后进先出(LIFO)特性,队列则遵循先进先出(FIFO)原则。例如,...
"两链表的第一个公共节点"(Intersection of Two Linked Lists)就是一个实例。 4. **二叉树**:二叉树结构常用于搜索和排序,LeetCode 中有许多涉及遍历、平衡、构造和修剪的问题。"二叉树的最大路径和"(Maximum ...
3. **链表**:链表问题考察了指针操作和递归能力,如"两链表的交点"(Intersection of Two Linked Lists)需要找到两个链表的第一个公共节点。 4. **栈和队列**:这两个数据结构常用于处理层次遍历和逆序操作。...
"两链表的交叉点"(Intersection of Two Linked Lists)是一道常见的链表题目。 4. **树**:涉及二叉树、平衡二叉树、红黑树等,题目涵盖遍历、搜索、构造等。"二叉树的最大路径和"(Maximum Depth of Binary Tree...
leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起...160.Intersection of Two Linke
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您...Intersection of Two Linke
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您...Intersection of Two Linke
160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的括号:valid-parentheses 150-逆波兰表达式求值:evaluate-reverse-polish-notation 155-最小栈:min-stack Tree ...
leetcode 盒子包装leetcode leetcode 上的解决方案 # 问题 关联 1 一维数组的运行总和 2 子矩形查询 3 洗牌 4 好的对数 5 拥有最多糖果的孩子 ...lcc-march2021/intersection-of-two-linked-lists 32 lcc-
Tricks of the Windows video Game Programming <br>PART I Windows Programming Foundations 7 1 Journey into the Abyss 9 A Little History.............................................................