forked from TheNewStyles/hackerrank-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertNodeAtSpecificPosition.cs
More file actions
68 lines (51 loc) · 2.19 KB
/
InsertNodeAtSpecificPosition.cs
File metadata and controls
68 lines (51 loc) · 2.19 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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
// Funtion Description Complete the function SinglyLinkedListNode in the editor. It must return a reference to the head node of your finished list.
// SinglyLinkedListNode 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
// Input Format
// The first line contains an integer , the number of elements in the linked list.
// Each of the next lines contains an integer node[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
// 3
// 16
// 13
// 7
// 1
// 2
// Sample Output
// 16 13 1 7
// Explanation
// The initial linked list is 16 13 7. We have to insert at the position which currently has in it. The updated linked list will be 16 13 1 7
// Complete the insertNodeAtPosition function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
var newNode = new SinglyLinkedListNode(data);
var currentNode = head;
var counter = 0;
while (counter < position-1)
{
currentNode = currentNode.next;
counter++;
}
newNode.next = currentNode.next;
currentNode.next = newNode;
return head;
}