We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b830d52 commit fe502b7Copy full SHA for fe502b7
kotlin/Fast and Slow Pointers/LinkedListMidpoint.kt
@@ -0,0 +1,16 @@
1
+/*
2
+ Definition of ListNode:
3
+ data class ListNode(var value: Int, var next: ListNode? = null)
4
+*/
5
+
6
+fun linkedListMidpoint(head: ListNode?): ListNode? {
7
+ var slow = head
8
+ var fast = head
9
+ // When the fast pointer reaches the end of the list, the slow
10
+ // pointer will be at the midpoint of the linked list.
11
+ while (fast != null && fast.next != null) {
12
+ slow = slow!!.next
13
+ fast = fast.next!!.next
14
+ }
15
+ return slow
16
+}
0 commit comments