Skip to content

Commit c1d02ed

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 02.07. 链表相交
1 parent a4986fb commit c1d02ed

File tree

5 files changed

+171
-61
lines changed

5 files changed

+171
-61
lines changed

Diff for: index.html

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<script>
3030
window.$docsify = {
3131
name: 'leetcode',
32-
// repo: 'doocs/leetcode',
3332
search: [
3433
'/','/solution/','/lcof/','lcci/'
3534
],

Diff for: lcci/02.07.Intersection of Two Linked Lists/README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,39 @@
66

77
## 解法
88
<!-- 这里可写通用的实现逻辑 -->
9-
9+
先求两链表的长度差 `differ`,接着较长的链表先走 `differ` 步,之后两链表同时走,若相遇,则说明相交。
1010

1111
### Python3
1212
<!-- 这里可写当前语言的特殊实现逻辑 -->
1313

1414
```python
15-
15+
# Definition for singly-linked list.
16+
# class ListNode:
17+
# def __init__(self, x):
18+
# self.val = x
19+
# self.next = None
20+
21+
class Solution:
22+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
23+
len1, len2 = self._length(headA), self._length(headB)
24+
if len1 < len2:
25+
headA, headB = headB, headA
26+
differ = abs(len1 - len2)
27+
for _ in range(differ):
28+
headA = headA.next
29+
while headA:
30+
if headA == headB:
31+
return headA
32+
headA = headA.next
33+
headB = headB.next
34+
return None
35+
36+
def _length(self, node: ListNode) -> int:
37+
n = 0
38+
while node:
39+
node = node.next
40+
n += 1
41+
return n
1642
```
1743

1844
### Java
+115-57
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,115 @@
1-
# [02.07. Intersection of Two Linked Lists](https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci)
2-
3-
## Description
4-
<p>Given two (singly) linked lists, determine if the two lists intersect. Return the inter&shy; secting node. Note that the intersection is defined based on reference, not value. That is, if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list, then they are intersecting.</p>
5-
6-
<p><strong>Example 1: </strong></p>
7-
8-
<pre>
9-
<strong>Input: </strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
10-
<strong>Output: </strong>Reference of the node with value = 8
11-
<strong>Input Explanation:</strong> The intersected node&#39;s value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.</pre>
12-
13-
<p><strong>Example 2: </strong></p>
14-
15-
<pre>
16-
<strong>Input: </strong>intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
17-
<strong>Output: </strong>Reference of the node with value = 2
18-
<strong>Input Explanation:</strong>&nbsp;The intersected node&#39;s value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.</pre>
19-
20-
<p><strong>Example 3: </strong></p>
21-
22-
<pre>
23-
<strong>Input: </strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
24-
<strong>Output: </strong>null
25-
<strong>Input Explanation:</strong> From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
26-
<strong>Explanation:</strong> The two lists do not intersect, so return null.</pre>
27-
28-
<p><b>Notes:</b></p>
29-
30-
<ul>
31-
<li>If the two linked lists have no intersection at all, return&nbsp;<code>null</code>.</li>
32-
<li>The linked lists must retain their original structure after the function returns.</li>
33-
<li>You may assume there are no cycles anywhere in the entire linked structure.</li>
34-
<li>Your code should preferably run in O(n) time and use only O(1) memory.</li>
35-
</ul>
36-
37-
38-
39-
## Solutions
40-
41-
42-
### Python3
43-
44-
```python
45-
46-
```
47-
48-
### Java
49-
50-
```java
51-
52-
```
53-
54-
### ...
55-
```
56-
57-
```
1+
# [02.07. Intersection of Two Linked Lists](https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci)
2+
3+
## Description
4+
<p>Given two (singly) linked lists, determine if the two lists intersect. Return the inter&shy; secting node. Note that the intersection is defined based on reference, not value. That is, if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list, then they are intersecting.</p>
5+
6+
7+
8+
<p><strong>Example 1: </strong></p>
9+
10+
11+
12+
<pre>
13+
14+
<strong>Input: </strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
15+
16+
<strong>Output: </strong>Reference of the node with value = 8
17+
18+
<strong>Input Explanation:</strong> The intersected node&#39;s value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.</pre>
19+
20+
21+
22+
<p><strong>Example 2: </strong></p>
23+
24+
25+
26+
<pre>
27+
28+
<strong>Input: </strong>intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
29+
30+
<strong>Output: </strong>Reference of the node with value = 2
31+
32+
<strong>Input Explanation:</strong>&nbsp;The intersected node&#39;s value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.</pre>
33+
34+
35+
36+
<p><strong>Example 3: </strong></p>
37+
38+
39+
40+
<pre>
41+
42+
<strong>Input: </strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
43+
44+
<strong>Output: </strong>null
45+
46+
<strong>Input Explanation:</strong> From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
47+
48+
<strong>Explanation:</strong> The two lists do not intersect, so return null.</pre>
49+
50+
51+
52+
<p><b>Notes:</b></p>
53+
54+
55+
56+
<ul>
57+
58+
<li>If the two linked lists have no intersection at all, return&nbsp;<code>null</code>.</li>
59+
60+
<li>The linked lists must retain their original structure after the function returns.</li>
61+
62+
<li>You may assume there are no cycles anywhere in the entire linked structure.</li>
63+
64+
<li>Your code should preferably run in O(n) time and use only O(1) memory.</li>
65+
66+
</ul>
67+
68+
69+
70+
71+
## Solutions
72+
73+
74+
### Python3
75+
76+
```python
77+
# Definition for singly-linked list.
78+
# class ListNode:
79+
# def __init__(self, x):
80+
# self.val = x
81+
# self.next = None
82+
83+
class Solution:
84+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
85+
len1, len2 = self._length(headA), self._length(headB)
86+
if len1 < len2:
87+
headA, headB = headB, headA
88+
differ = abs(len1 - len2)
89+
for _ in range(differ):
90+
headA = headA.next
91+
while headA:
92+
if headA == headB:
93+
return headA
94+
headA = headA.next
95+
headB = headB.next
96+
return None
97+
98+
def _length(self, node: ListNode) -> int:
99+
n = 0
100+
while node:
101+
node = node.next
102+
n += 1
103+
return n
104+
```
105+
106+
### Java
107+
108+
```java
109+
110+
```
111+
112+
### ...
113+
```
114+
115+
```
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
len1, len2 = self._length(headA), self._length(headB)
10+
if len1 < len2:
11+
headA, headB = headB, headA
12+
differ = abs(len1 - len2)
13+
for _ in range(differ):
14+
headA = headA.next
15+
while headA:
16+
if headA == headB:
17+
return headA
18+
headA = headA.next
19+
headB = headB.next
20+
return None
21+
22+
def _length(self, node: ListNode) -> int:
23+
n = 0
24+
while node:
25+
node = node.next
26+
n += 1
27+
return n

Diff for: lcof/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Interview questions from the most popular companies in the IT industry are taken
55

66
Readers will improve their interview performance after reading this book. It will be beneficial for them even after they get offers, because its topics, such as approaches to analyzing difficult problems, writing robust code and optimizing, are all essential for high-performing coders.
77

8-
[中文文档](/solution/README.md)
8+
[中文文档](/lcof/README.md)
99

1010
## Solutions
1111
English Solutions is not available for the book. Please switch to Chinese version.

0 commit comments

Comments
 (0)