Skip to content

Commit e13374b

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0160
1 parent 5a9cd15 commit e13374b

File tree

6 files changed

+104
-31
lines changed

6 files changed

+104
-31
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
1. [重排链表](/solution/0100-0199/0143.Reorder%20List/README.md)
6666
1. [旋转链表](/solution/0000-0099/0061.Rotate%20List/README.md)
6767
1. [回文链表](/solution/0200-0299/0234.Palindrome%20Linked%20List/README.md)
68+
1. [相交链表](/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/README.md)
6869
1. [环形链表](/solution/0100-0199/0141.Linked%20List%20Cycle/README.md)
6970
1. [环形链表 II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README.md)
7071

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6363
1. [Reorder List](/solution/0100-0199/0143.Reorder%20List/README_EN.md)
6464
1. [Rotate List](/solution/0000-0099/0061.Rotate%20List/README_EN.md)
6565
1. [Palindrome Linked List](/solution/0200-0299/0234.Palindrome%20Linked%20List/README_EN.md)
66+
1. [Intersection of Two Linked Lists](/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/README_EN.md)
6667
1. [Linked List Cycle](/solution/0100-0199/0141.Linked%20List%20Cycle/README_EN.md)
6768
1. [Linked List Cycle II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README_EN.md)
6869

solution/0100-0199/0160.Intersection of Two Linked Lists/README.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,63 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
定义两个指针,遍历时,当两个指针到达末尾的节点指向另一个链表的头部继续遍历,最后如果相遇则为交点(在第一轮移动中恰好抹除了长度差)。
66+
67+
两个指针等于移动了相同的距离,有交点就返回,无交点就是各走了两条指针的长度。
68+
6569
<!-- tabs:start -->
6670

6771
### **Python3**
6872

6973
<!-- 这里可写当前语言的特殊实现逻辑 -->
7074

7175
```python
72-
76+
# Definition for singly-linked list.
77+
# class ListNode:
78+
# def __init__(self, x):
79+
# self.val = x
80+
# self.next = None
81+
82+
class Solution:
83+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
84+
if headA is None or headB is None:
85+
return None
86+
pA, pB = headA, headB
87+
while pA != pB:
88+
pA = pA.next if pA else headB
89+
pB = pB.next if pB else headA
90+
return pA
7391
```
7492

7593
### **Java**
7694

7795
<!-- 这里可写当前语言的特殊实现逻辑 -->
7896

7997
```java
80-
98+
/**
99+
* Definition for singly-linked list.
100+
* public class ListNode {
101+
* int val;
102+
* ListNode next;
103+
* ListNode(int x) {
104+
* val = x;
105+
* next = null;
106+
* }
107+
* }
108+
*/
109+
public class Solution {
110+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
111+
if (headA == null || headB == null) {
112+
return null;
113+
}
114+
ListNode pA = headA, pB = headB;
115+
while (pA != pB) {
116+
pA = pA != null ? pA.next : headB;
117+
pB = pB != null ? pB.next : headA;
118+
}
119+
return pA;
120+
}
121+
}
81122
```
82123

83124
### **...**

solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,50 @@
7878
### **Python3**
7979

8080
```python
81-
81+
# Definition for singly-linked list.
82+
# class ListNode:
83+
# def __init__(self, x):
84+
# self.val = x
85+
# self.next = None
86+
87+
class Solution:
88+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
89+
if headA is None or headB is None:
90+
return None
91+
pA, pB = headA, headB
92+
while pA != pB:
93+
pA = pA.next if pA else headB
94+
pB = pB.next if pB else headA
95+
return pA
8296
```
8397

8498
### **Java**
8599

86100
```java
87-
101+
/**
102+
* Definition for singly-linked list.
103+
* public class ListNode {
104+
* int val;
105+
* ListNode next;
106+
* ListNode(int x) {
107+
* val = x;
108+
* next = null;
109+
* }
110+
* }
111+
*/
112+
public class Solution {
113+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
114+
if (headA == null || headB == null) {
115+
return null;
116+
}
117+
ListNode pA = headA, pB = headB;
118+
while (pA != pB) {
119+
pA = pA != null ? pA.next : headB;
120+
pB = pB != null ? pB.next : headA;
121+
}
122+
return pA;
123+
}
124+
}
88125
```
89126

90127
### **...**

solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.java

+5-27
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,11 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
1414
if (headA == null || headB == null) {
1515
return null;
1616
}
17-
int lenA = 0, lenB = 0;
18-
ListNode p = headA, q = headB;
19-
while (p != null) {
20-
p = p.next;
21-
++lenA;
17+
ListNode pA = headA, pB = headB;
18+
while (pA != pB) {
19+
pA = pA != null ? pA.next : headB;
20+
pB = pB != null ? pB.next : headA;
2221
}
23-
while (q != null) {
24-
q = q.next;
25-
++lenB;
26-
}
27-
28-
p = headA;
29-
q = headB;
30-
if (lenA > lenB) {
31-
for (int i = 0; i < lenA - lenB; ++i) {
32-
p = p.next;
33-
}
34-
} else {
35-
for (int i = 0; i < lenB - lenA; ++i) {
36-
q = q.next;
37-
}
38-
}
39-
while (p != null && p != q) {
40-
p = p.next;
41-
q = q.next;
42-
}
43-
return p;
44-
22+
return pA;
4523
}
4624
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
9+
if headA is None or headB is None:
10+
return None
11+
pA, pB = headA, headB
12+
while pA != pB:
13+
pA = pA.next if pA else headB
14+
pB = pB.next if pB else headA
15+
return pA

0 commit comments

Comments
 (0)