Skip to content

Commit fe502b7

Browse files
committed
Add Linked List Midpoint solution
1 parent b830d52 commit fe502b7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)