File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,36 @@ public void insertHead(int x){
34
34
head = newNode ; //Now set the new link to be the head
35
35
}
36
36
37
+
38
+ /**
39
+ * Inserts a new node at a specified position
40
+ * @param head head node of the linked list
41
+ * @param data data to be stored in a new node
42
+ * @param position position at which a new node is to be inserted
43
+ * @return reference of the head of the linked list
44
+ */
45
+
46
+ Node InsertNth (Node head , int data , int position ) {
47
+
48
+ Node newNode = new Node ();
49
+ newNode .data = data ;
50
+
51
+ if (position == 0 ) {
52
+ newNode .next = head ;
53
+ return newNode ;
54
+ }
55
+
56
+ Node current = head ;
57
+
58
+ while (--position > 0 ) {
59
+ current = current .next ;
60
+ }
61
+
62
+ newNode .next = current .next ;
63
+ current .next = newNode ;
64
+ return head ;
65
+ }
66
+
37
67
/**
38
68
* This method deletes an element at the head
39
69
*
You can’t perform that action at this time.
0 commit comments