To insert a new node after a given node prev: newNode = new ListNode(val) newNode.next = prev.next prev.next = newNode Order matters here. If you set prev.next = newNode first, you lose the reference to the rest of the list.
Inserting at the head is a special case: newNode = new ListNode(val) newNode.next = head head = newNode Both operations are assuming you already have a reference to the insertion point.
Finding that point might take . Time: . Space: .