In this Insert a node at a specific position in a linked list HackerRank solution, Given the 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 attribute, 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.
Example
refers to the first node in the list
Insert a node at position with . The new list is
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
Returns
- SinglyLinkedListNode pointer: a reference to the head of the revised list
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 next line contains an integer , the data of the node that is to be inserted.
The last line contains an integer .
Constraints
- , where is the element of the linked list.
- .
Sample Input
3
16
13
7
1
2
Sample Output
16 13 1 7
Explanation
The initial linked list is . Insert at the position which currently has in it. The updated linked list is .
Insert a node at a specific position in a linked list HackerRank solution
I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.
Problem Solution in Python
def insertNodeAtPosition(llist, data, position):
# Write your code here
temp = llist
prev = None
count = -1
while count <= position:
count += 1
if count == position:
node = SinglyLinkedListNode(data)
node.next = temp
if prev is not None:
prev.next = node
break
prev = temp
temp = temp.next
return llist
Problem Solution in JavaScript
function insertNodeAtPosition(llist, data, position) {
// Write your code here
let index = 0, curr = llist;
while (index <= position) {
let next = curr.next;
if (index == position) {
let currData = curr.data;
curr.data = data;
let a = {};
curr.next = a;
a.data = currData;
a.next = next;
break;
}
index++;
curr = next;
}
return llist;
}
Problem Solution in Java
public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int pos) {
SinglyLinkedListNode newNode=new SinglyLinkedListNode(data);
if(pos==0){
newNode.next=head;
head=newNode;
}
else{
SinglyLinkedListNode temp=head;
SinglyLinkedListNode next=null;
for(int i=0;i<pos-1;i++){
temp=temp.next;
}
newNode.data=data;
next=temp.next;
temp.next=newNode;
newNode.next=next;
}
return head;
}
Problem Solution in C#
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position)
{
SinglyLinkedListNode nextNode = llist;
int i = 1;
while (i < position) {
nextNode = nextNode.next;
i++;
}
SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);
newNode.next = nextNode.next;
nextNode.next = newNode;
return llist;
}
Leave a Reply