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.
As an example, if your list starts as and you want to insert a node at position with , your new list should be
Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.
insertNodeAtPosition has the following parameters:
- head: a SinglyLinkedListNode pointer to the head of the list
- data: an integer value to insert as data in your new node
- position: an integer position to insert the new node, zero based indexing
Input Format
The first line contains an integer , the number of elements in the linked list.
Each of the next lines contains an integer SinglyLinkedListNode[i].data.
The last line contains an integer .
Each of the next lines contains an integer SinglyLinkedListNode[i].data.
The last line contains an integer .
Constraints
- , where is the element of the linked list.
- .
Output Format
Return a reference to the list head. Locked code prints the list for you.
Sample Input
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
SinglyLinkedListNode node = new SinglyLinkedListNode(data);
//Start Index
if (head == null)
head = node;
//End of the single linked list
// 0 1 2
var lastPosition = 0;
var current = head;
while (current.next != null)
{
lastPosition++;
var temp = current;
current = current.next;
//Insert into Middle position
if (lastPosition == position)
{
temp.next = node;
node.next = current;
}
//Insert into Last position
if (current.next == null)
{
lastPosition++;
if (lastPosition == position)
{
temp.next = node;
node.next = current;
}
else if (lastPosition < position)
{
current.next = node;
}
}
}
if (lastPosition < position)
{
current.next = node;
}
return head;
}
No comments:
Post a Comment