File tree 3 files changed +87
-18
lines changed
lcci/02.07.Intersection of Two Linked Lists
3 files changed +87
-18
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,48 @@ class Solution:
45
45
<!-- 这里可写当前语言的特殊实现逻辑 -->
46
46
47
47
``` 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
+ }
48
80
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
+ }
49
90
```
50
91
51
92
### ...
Original file line number Diff line number Diff line change 48
48
<strong >Explanation:</strong > The two lists do not intersect, so return null.</pre >
49
49
50
50
51
-
52
51
<p ><b >Notes:</b ></p >
53
52
54
-
55
-
56
- <ul >
57
-
58
- <li>If the two linked lists have no intersection at all, return <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  ; <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.
70
57
71
58
## Solutions
72
59
@@ -106,7 +93,48 @@ class Solution:
106
93
### Java
107
94
108
95
``` 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
+ }
110
138
```
111
139
112
140
### ...
You can’t perform that action at this time.
0 commit comments