Skip to content

Commit 1605b8f

Browse files
author
刘勋
committed
环形链表 II
1 parent e125430 commit 1605b8f

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.algorithm.study.demo.algorithm.leetcode;
2+
3+
/**
4+
* @author xun2.liu
5+
* @title: Solution6
6+
* @projectName algorithm-study
7+
* @description: 编写一个程序,找到两个单链表相交的起始节点。
8+
* 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
9+
* 输出:Reference of the node with value = 8
10+
*
11+
* 来源:力扣(LeetCode)
12+
* 链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists
13+
* @date 2020/5/18 19:35
14+
*/
15+
public class Solution6 {
16+
public static class ListNode {
17+
int val;
18+
ListNode next;
19+
ListNode(int x) { val = x; }
20+
}
21+
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
22+
//使用两个指针分别指向headA、headB
23+
//同时遍历两个连表
24+
//当headA遍历完后指针指向headB,当headB遍历完后指针指向headA
25+
//如此循环当两个指正都为Null的话代表没有相交的节点。
26+
//如果都两个指针对应的节点相等就返回相等的节点就是相交的节点
27+
ListNode p1=headA;
28+
ListNode p2=headB;
29+
while(p1!=p2){
30+
p1=p1==null?headB:p1.next;
31+
p2=p2==null?headA:p2.next;
32+
}
33+
return p1;
34+
}
35+
36+
public static void main(String[] args) {
37+
ListNode a=new ListNode(5);
38+
ListNode b=new ListNode(4);
39+
a.next=b;
40+
ListNode c=new ListNode(6);
41+
ListNode intersectionNode = getIntersectionNode(a, b);
42+
if (null!=intersectionNode){
43+
System.out.println(intersectionNode.val);
44+
}else{
45+
System.out.println("没有相交的节点哦");
46+
}
47+
48+
}
49+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.algorithm.study.demo.algorithm.leetcode;
2+
3+
/**
4+
* @author xun2.liu
5+
* @title: Solution7
6+
* @projectName algorithm-study
7+
* @description: 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
8+
*
9+
* 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
10+
*
11+
* 说明:不允许修改给定的链表。
12+
*
13+
* 示例 1:
14+
*
15+
* 输入:head = [1,2], pos = 0
16+
* 输出:tail connects to node index 0
17+
* 解释:链表中有一个环,其尾部连接到第一个节点。
18+
*
19+
* 来源:力扣(LeetCode)
20+
* 链接:https://leetcode-cn.com/problems/linked-list-cycle-ii
21+
*
22+
* @date 2020/5/19 17:16
23+
*/
24+
public class Solution7 {
25+
public static class ListNode {
26+
int val;
27+
ListNode next;
28+
ListNode(int x) { val = x; }
29+
}
30+
31+
/**
32+
* 快慢指针遍历连表。看是否相遇。如果相遇在判断是否是循环链表。
33+
* @param head
34+
* @return
35+
*/
36+
public static ListNode detectCycle(ListNode head) {
37+
if (null== head || head.next==null){
38+
return null;
39+
}
40+
ListNode p1=head;
41+
ListNode p2=head;
42+
while(p2!=null && p2.next!=null){
43+
p1=p1.next;
44+
p2=p2.next.next;
45+
if(p1==p2){
46+
break;
47+
}
48+
}
49+
if (p1!=p2){
50+
return null;
51+
}
52+
p1=head;
53+
while(p1!=p2){
54+
p1=p1.next;
55+
p2=p2.next;
56+
}
57+
return p1;
58+
}
59+
public static void main(String[] args) {
60+
ListNode a=new ListNode(5);
61+
ListNode b=new ListNode(4);
62+
ListNode c=new ListNode(6);
63+
ListNode d=new ListNode(-1);
64+
a.next=b;
65+
b.next=c;
66+
c.next=b;
67+
// c.next=b;
68+
ListNode listNode = detectCycle(a);
69+
System.out.println(listNode==null?"":listNode.val);
70+
}
71+
}

0 commit comments

Comments
 (0)