Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf92e820

Browse files
refactor 160
1 parentadb3b2d commitf92e820

File tree

1 file changed

+4
-66
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+4
-66
lines changed

‎src/main/java/com/fishercoder/solutions/_160.java

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,6 @@
55
importjava.util.HashSet;
66
importjava.util.Set;
77

8-
/**
9-
* 160. Intersection of Two Linked Lists
10-
Write a program to find the node at which the intersection of two singly linked lists begins.
11-
12-
13-
For example, the following two linked lists:
14-
15-
A: a1 → a2
16-
17-
c1 → c2 → c3
18-
19-
B: b1 → b2 → b3
20-
begin to intersect at node c1.
21-
22-
Example 1:
23-
A: 4 → 1
24-
25-
8 → 4 → 5
26-
27-
B: 5 → 0 → 1
28-
29-
30-
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
31-
Output: Reference of the node with value = 8
32-
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
33-
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].
34-
There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
35-
36-
37-
Example 2:
38-
A: 0 -> 9 → 1
39-
40-
2 → 4
41-
42-
B: 3
43-
44-
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
45-
Output: Reference of the node with value = 2
46-
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
47-
From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4].
48-
There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
49-
50-
51-
Example 3:
52-
A: 2 → 6 -> 4
53-
54-
B: 1 -> 5
55-
56-
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
57-
Output: null
58-
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5].
59-
Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
60-
Explanation: The two lists do not intersect, so return null.
61-
62-
63-
Notes:
64-
65-
If the two linked lists have no intersection at all, return null.
66-
The linked lists must retain their original structure after the function returns.
67-
You may assume there are no cycles anywhere in the entire linked structure.
68-
Your code should preferably run in O(n) time and use only O(1) memory.
69-
*/
70-
718
publicclass_160 {
729

7310
publicstaticclassSolution1 {
@@ -108,10 +45,11 @@ private int findLen(ListNode head) {
10845
publicstaticclassSolution2 {
10946
/**
11047
* Most optimal solution:
111-
*
48+
* <p>
11249
* O(m+n) time
11350
* O(1) space
114-
* credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len*/
51+
* credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len
52+
*/
11553
publicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB) {
11654
if (headA ==null ||headB ==null) {
11755
returnnull;
@@ -134,7 +72,7 @@ public static class Solution3 {
13472
/**
13573
* O(m+n) time
13674
* O(Math.max(m, n)) space
137-
* */
75+
*/
13876
publicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB) {
13977
Set<ListNode>set =newHashSet<>();
14078
while (headA !=null) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp