Skip to content

Commit 7de92ba

Browse files
committed
docs: update README.md
1 parent c1d02ed commit 7de92ba

File tree

3 files changed

+87
-18
lines changed

3 files changed

+87
-18
lines changed

lcci/02.07.Intersection of Two Linked Lists/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,48 @@ class Solution:
4545
<!-- 这里可写当前语言的特殊实现逻辑 -->
4646

4747
```java
48+
/**
49+
* Definition for singly-linked list.
50+
* public class ListNode {
51+
* int val;
52+
* ListNode next;
53+
* ListNode(int x) {
54+
* val = x;
55+
* next = null;
56+
* }
57+
* }
58+
*/
59+
public class Solution {
60+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
61+
int len1 = len(headA), len2 = len(headB);
62+
int differ = Math.abs(len1 - len2);
63+
if (len1 < len2) {
64+
ListNode t = headA;
65+
headA = headB;
66+
headB = t;
67+
}
68+
while (differ-- > 0) {
69+
headA = headA.next;
70+
}
71+
while (headA != null) {
72+
if (headA == headB) {
73+
return headA;
74+
}
75+
headA = headA.next;
76+
headB = headB.next;
77+
}
78+
return null;
79+
}
4880

81+
private int len(ListNode node) {
82+
int n = 0;
83+
while (node != null) {
84+
node = node.next;
85+
++n;
86+
}
87+
return n;
88+
}
89+
}
4990
```
5091

5192
### ...

lcci/02.07.Intersection of Two Linked Lists/README_EN.md

+46-18
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,12 @@
4848
<strong>Explanation:</strong> The two lists do not intersect, so return null.</pre>
4949

5050

51-
5251
<p><b>Notes:</b></p>
5352

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-
53+
- If the two linked lists have no intersection at all, return&nbsp;<code>null</code>.
54+
- The linked lists must retain their original structure after the function returns.
55+
- You may assume there are no cycles anywhere in the entire linked structure.
56+
- Your code should preferably run in O(n) time and use only O(1) memory.
7057

7158
## Solutions
7259

@@ -106,7 +93,48 @@ class Solution:
10693
### Java
10794

10895
```java
109-
96+
/**
97+
* Definition for singly-linked list.
98+
* public class ListNode {
99+
* int val;
100+
* ListNode next;
101+
* ListNode(int x) {
102+
* val = x;
103+
* next = null;
104+
* }
105+
* }
106+
*/
107+
public class Solution {
108+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
109+
int len1 = len(headA), len2 = len(headB);
110+
int differ = Math.abs(len1 - len2);
111+
if (len1 < len2) {
112+
ListNode t = headA;
113+
headA = headB;
114+
headB = t;
115+
}
116+
while (differ-- > 0) {
117+
headA = headA.next;
118+
}
119+
while (headA != null) {
120+
if (headA == headB) {
121+
return headA;
122+
}
123+
headA = headA.next;
124+
headB = headB.next;
125+
}
126+
return null;
127+
}
128+
129+
private int len(ListNode node) {
130+
int n = 0;
131+
while (node != null) {
132+
node = node.next;
133+
++n;
134+
}
135+
return n;
136+
}
137+
}
110138
```
111139

112140
### ...

lcci/02.07.Intersection of Two Linked Lists/Solution.java

Whitespace-only changes.

0 commit comments

Comments
 (0)