Insert a node at a specific position in a Linked List This code is built to insert a Node into a specific position of a linked-list. It passes all the test cases for the interview, but I was wondering about the coding style regarding techniques, and especially variable naming, and any other best-practices you can help with.

Node* InsertNth(Node *head, int data, int position)
{
    // Complete this method only
    // Do not write main function. 
    Node * sentinel = new Node();
    sentinel-&gt;data = -1;
    sentinel-&gt;next = head;
    Node* node = sentinel;
    int i = 0;
    while (node-&gt;next &amp;&amp; i &lt; position) {
        node = node-&gt;next;
        i++;
    }

    Node* preNext = node-&gt;next;
    Node* newNode = new Node();
    newNode-&gt;data = data;
    newNode-&gt;next = preNext;
    node-&gt;next = newNode;
    head = sentinel-&gt;next;
    delete sentinel;

    return head;

}
This is how I would have done it:

Node* InsertNth(Node *head, int data, int position)
{
    // Use a sentinel as this leads to simpler code
    // and no need to test for the empty list case.
    Node sentinel{-1, head};

    // Find the position we want to insert in.
    // Note: If there are less than `position` elements then
    //       the new element will be added to the end.
    Node* current = &amp;sentinel;
    for(; position != 0 &amp;&amp; current-&gt;next;--position, current = current-&gt;next)
    {}

    // Create the new Node and insert it into the chain.
    Node* newNode = new Node{data, current-&gt;next};
    current-&gt;next = newNode;

    // Update head.
    // In the case of null list or inserting at position 0
    // this will do something. Otherwise this is a no-op.
    head = sentinel.next;

    return newNode;
}


Notes:


Make notes of assumptions you have made in the comments.
I like the for() expression over the while() expression.
It expresses my intent more clearly (ie that I am just chaining along the list).
No need to dynamically create the sentinel. By declaring it on the stack. It works better.
I have assumed node has a reasonable constructor to make the code cleaner.