Sakshi Singh
1 min readDec 28, 2018

HACKERRANK SOLUTION: Insert a node at a specific position in a linked list

//COPY PASTE THIS PART OF CODE IN THE GIVEN BLANK SPACE OF YOUR EDITOR….

static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {

SinglyLinkedListNode n = new SinglyLinkedListNode(data);

n.data = data;

n.next = null;

if(head==null)

{

n.next=head;

head = n;

return head;

}

else

{

SinglyLinkedListNode n1 = head;

for(int i=0;i<position-1;i++)

{

n1 = n1.next;

}

n.next=n1.next;

n1.next=n;

return head;

}

}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sakshi Singh
Sakshi Singh

No responses yet

Write a response