Insert into the Middle of a Linked List
Problem statement
Insert one new node with value after the first ceil(n/2) original nodes, where n is the list length. For even n, this is between the two halves; for odd n, insert immediately after the central node. For n=0, create the head. Preserve the original node order and return the updated head.
Function
insertAtMiddle(head: ListNode, value: int) → ListNodeExamples
Example 1
head = [1,2,3,4]value = 9return = [1,2,9,3,4]For length4, the new node follows the first2original nodes.
Example 2
head = [1,2,3]value = 9return = [1,2,9,3]For length3, the central node is2 and the new node follows it.
Constraints
0 <= head.length <= 500.-1000000 <= head[i] <= 1000000.-1000000 <= value <= 1000000.- The input list is acyclic.