File tree Expand file tree Collapse file tree 2 files changed +51
-2
lines changed
leetcode/intersection-of-two-linked-lists Expand file tree Collapse file tree 2 files changed +51
-2
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode.`intersection-of-two-linked-lists`
2
+
3
+ import ListNode
4
+ import org.junit.Test
5
+ import util.ListNodeUtil
6
+
7
+ class Solution {
8
+ fun getIntersectionNode (headA : ListNode ? , headB : ListNode ? ): ListNode ? {
9
+ var pA = headA
10
+ while (pA?.next != null ) {
11
+ if (headB == pA) return headB
12
+ pA = pA.next
13
+ }
14
+ if (pA == headB) return headB
15
+ pA?.next = headA
16
+ val endA = pA
17
+ var slowP = headB
18
+ var fastP = headB
19
+ while ((slowP != fastP && slowP != null && fastP != null ) || slowP == headB) {
20
+ slowP = slowP?.next
21
+ fastP = fastP?.next?.next
22
+ }
23
+ slowP = headB
24
+ while ((slowP != fastP && slowP != null && fastP != null ) || slowP == headB) {
25
+ slowP = slowP?.next
26
+ fastP = fastP?.next
27
+ }
28
+ endA?.next = null
29
+ return fastP
30
+ }
31
+
32
+ @Test
33
+ fun test (){
34
+ val list1 = ListNodeUtil .generate(intArrayOf(8 ,4 ,5 ))
35
+ }
36
+ }
Original file line number Diff line number Diff line change 1
1
package util
2
2
3
- object ListNodeUtil {
4
- fun generate (){
3
+ import ListNode
5
4
5
+ object ListNodeUtil {
6
+ fun generate (intArray : IntArray ): ListNode {
7
+ lateinit var head: ListNode
8
+ var tmp: ListNode ? = null
9
+ intArray.forEachIndexed { index, i ->
10
+ if (index == 0 ) {
11
+ head = ListNode (i)
12
+ tmp = head
13
+ } else {
14
+ tmp?.next = ListNode (i)
15
+ tmp = tmp?.next
16
+ }
17
+ }
18
+ return head
6
19
}
7
20
}
You can’t perform that action at this time.
0 commit comments