-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeInsertionToLinkedList.js
More file actions
28 lines (22 loc) · 1.3 KB
/
nodeInsertionToLinkedList.js
File metadata and controls
28 lines (22 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* 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
*/
function insertNodeAtPosition(head, data, position) {
let currentNode = head;
for (let i = 0; i < position; i++){
if (i == position-1) {
let newNode = new SinglyLinkedListNode(data);
newNode.next = currentNode.next;
currentNode.next = newNode;
}
currentNode = currentNode.next;
}
return head;
}