Linked List Delete Middle Node
Last updated
Last updated
You are given the head
of a linked list. Delete the middle node, and return the head
of the modified linked list.
The middle node of a linked list of size n is the βn / 2βth node from the start using 0-based indexing, where βxβ denotes the largest integer less than or equal to x.
The time complexity of the DeleteMiddle
algorithm is O(n), where n is the number of nodes in the linked list. This is because each node in the list is visited exactly once.
The space complexity of the algorithm is O(1). This is because the algorithm only uses a constant amount of space to store the pointers (fast
, slow
), regardless of the size of the input linked list. The algorithm modifies the input linked list in-place and does not use any additional data structures whose size depends on the input. Hence, the space complexity is constant.