Skip to content

Files

Latest commit

 

History

History
19 lines (18 loc) · 574 Bytes

2095.delete-the-middle-node-of-a-linked-list.md

File metadata and controls

19 lines (18 loc) · 574 Bytes
fun deleteMiddle(head: ListNode?): ListNode? {
    val sentinel = ListNode(-1)
    sentinel.next = head
    var slow: ListNode? = sentinel
    var fast: ListNode? = sentinel
    // Previous node before the slow pointer
    var previous: ListNode? = sentinel
    while (fast?.next != null) {
        previous = slow
        slow = slow?.next
        fast = fast?.next?.next
    }
    previous?.next = slow?.next
    return sentinel.next
}