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.
/** * 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) { if (headA == null || headB == null) { return null; } ListNode cur1 = headA; ListNode cur2 = headB; int n = 0; while (cur1.next != null) { n++; cur1 = cur1.next; } while (cur2.next != null) { n--; cur2 = cur2.next; } if (cur1 != cur2) { return null; } cur1 = n > 0 ? headA : headB; cur2 = cur1 == headA ? headB : headA; n = Math.abs(n); while (n != 0) { n--; cur1 = cur1.next; } while (cur1 != cur2) { cur1 = cur1.next; cur2 = cur2.next; } return cur1; } }
相关推荐
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
两个大数组的交集
350. 两个数组的交集II Intersection of Two Arrays II思路一:排序找交集,从小到大遍历,遇到相等的就加到ans中,然后都看下一
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 | ...
The topics include Cycle and Intersection Detection in Linked Lists, Finding the First Node of a Cycle, Generation of Permutations and Combinations with and without Repetition, Reconstruction of ...
smarter-decisions-intersection-of-iot.pdf
Randy B. Lee & David A. Fredricks的intersection of parametric surfaces and plane
信息安全_数据安全_Intersection of Data Breach Noti 常规渗透 可信编译 态势感知 安全审计 企业安全
(2)The Intersection of two Sets (overload operator *,*=) (3)The Difference of two Sets (overload operator -) (4)Add an element to a Set (5)Delete an element from a Set (6)Get the element which has the...
Verse Communications84 La EspiralOrinda, CA 94563 USAnate@verse.comABSTRACTSorted lists of integers are commonly used in inverted in- dexes and database systems. They are often compres
Intersection-of-Two-Arrays-LeetCode
(2)The Intersection of two Sets (overload operator *,*=) (3)The Difference of two Sets (overload operator -) (4)Add an element to a Set (5)Delete an element from a Set (6)Get the element which has...
(2)The Intersection of two Sets (overload operator *,*=) (3)The Difference of two Sets (overload operator -) (4)Add an element to a Set (5)Delete an element from a Set (6)Get the element...
CIRCLEINTERSECT Finds intersection of two circles.Function to return the intersection points between two circles given their centres and radi.
多支链并联机器人自由度分析与驱动器布置,卜王辉,刘振宇,为了克服经典CGK自由度计算公式在处理多约束多支链并联机器人的例外情况,本文提出了一种新的基于螺旋空间交集运算的机构自由度计
leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起...160.Intersection of Two Linke
You have two sets of data, two lists, and you want to compare them. We can demonstrate this problem with the awesome power of a Venn diagram: The Venn diagram has three interesting areas: the bit on ...
A.1 The Intersection of Two Vector Spaces 459 A.2 The Sum of Two Vector Spaces 460 A.3 The Cartesian Product of Two Vector Spaces 461 A.4 The Tensor Product of Two Vector Spaces 461 A.5 The Kronecker ...
This volume discusses a construction situated at the intersection of two different mathematical fields: Abstract harmonic analysis, understood as the theory of group representations and their ...
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您...Intersection of Two Linke