LeetCode Intersection of Two Linked Lists (找交叉点)

题意:给两个链表,他们可能后部分有重叠的地方(重叠是指,两链表都有元素处于同一块地址上),一旦重叠到就不可能分开啦。问在哪重叠的?当然是返回地址了。不重叠就返回null。

思路:用O(n)和O(1)的做法。

(1)若两串会重叠,那么到最后一个元素必定会相遇。

(2)先判断是否有重叠。

(3)有重叠了,要考虑两串长度不一样。后段肯定一样,不重叠的那一段可能不一样。要让它们同长才行,使得两个指针每次只走1步才会相遇。

(4)交叉遍历一次就可以使得两个指针处于那样的位置啦。一个指向短链的开头,一个指向长链的前段部分中的某个位置。

不是很简洁的代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12         ListNode *L1=headA;
13         ListNode *L2=headB;
14         //若无交点,且AB等长,那么他们会在null处相遇,退出。
15         //先判断是否有交点先。
16         while(L1&&L1->next) L1=L1->next;
17         while(L2&&L2->next) L2=L2->next;
18         if(L1!=L2)  return NULL;
19         L1=headA;
20         L2=headB;
21         while(L1!=L2)
22         {
23             if(L1)  L1=L1->next;
24             else    L1=headB;
25
26             if(L2)  L2=L2->next;
27             else L2=headA;
28         }
29         return L1;
30     }
31 };

AC代码

很傻逼但AC的代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12         ListNode *p=headA;
13         while(p)  p->val*=-1,p=p->next;
14
15         ListNode *q=headB;
16         while(q&&q->val>=0  ) q=q->next;
17
18         p=headA;
19         while(p)  p->val*=-1,p=p->next;
20
21
22         if(q)   return q;
23         else 0;
24
25
26     }
27 };

AC代码

简洁的代码:

时间: 2024-11-05 20:41:34

LeetCode Intersection of Two Linked Lists (找交叉点)的相关文章

LeetCode—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: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

leetcode Intersection of Two Linked Lists python

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: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. python

[LeetCode] Intersection of Two Linked Lists

Question: 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. 1.题型分类: 2.思路: 3.时间复杂度:O(n) 4.代

LeetCode——Intersection of Two Linked Lists

Description: 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. 求两个链表的相交的部分 /** * Definitio

[LeetCode] 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: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

[LeetCode] 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: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

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. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5],

LeetCode Intersection of Two Linked Lists 解题报告

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 求两个链表的第一个公共节点,如果不存在公共节点的话就返回null. A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 解题思路: 1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点. 2)分别求出两个链表的长度,然后对长度长的链表向前移动:LengA - LengB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节

[leetcode]Intersection of Two Linked Lists —— 熟悉python

好久没有耍代码了,手有点生,还有点痒,and 脑子有点锈.话说这个简单的东西,耍了我一个小时. 主要是最近很少碰代码.二是对python不熟悉,刚刚接触,但是顿时很热爱这个小蟒蛇.于是打算好好蹂躏它. 题目链接:https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 我的思路是这样的. 首先没有循环,所以不用判断是否绕一圈回来了.其次,看题意不会有多次分支,即分支只有一个,且一直相同到尾部.那么好办了. 我只需要逆向判