Insert a Node at a Specific Position in a Linked ListEasy
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
Given a pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its \(data\) attribute, insert this node at the desired position, and return the head node.
A position of 0 indicates the head, a position of 1 indicates one node away from the head, and so on. The head pointer given may be null, meaning that the initial list is empty.
Example
\(head\) refers to the first node in the list \(1 \to 2 \to 3\)
\(data = 4\)
\(position = 2\)
Insert a node at position \(2\) with \(data = 4\). The new list is \(1 \to 2 \to 4 \to 3\)