This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node. A position of 0 indicates 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.
You have to complete the SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) method which takes three arguments - the head of the linked list, the integer to insert and the position at which the integer must be inserted. You should NOT read any input from stdin/console. position will always be between 0 and (inclusive).
The first line contains an integer , the number of elements in the linked list.
The next lines contain an integer each, denoting the elements of the linked list in the order.
The next line contains an integer denoting the data that needs to be inserted into the linked list.
The last line contains an integer which is the position at which the should be inserted.
, where is the element of the linked list.
Insert the new node at the desired position and return the head of the updated linked list. Do NOT print anything to stdout/console.
The code in the editor prints the elements of the linked list in a single line separated by space.
16 13 1 7
The initial linked list is 16 13 7. We have to insert at the position which currently has 7. So, the updated linked list will be 16 13 1 7