File tree 1 file changed +47
-0
lines changed
Course 2 - Data Structures in JAVA/Lecture 7 - Linked Lists I
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -38,3 +38,50 @@ Sample Output 2 :
38
38
20 3 4 5 2 6 1 9
39
39
10 98 7 66 8 99
40
40
*/
41
+ /*
42
+
43
+ Following is the Node class already written for the Linked List
44
+
45
+ class LinkedListNode<T> {
46
+ T data;
47
+ LinkedListNode<T> next;
48
+
49
+ public LinkedListNode(T data) {
50
+ this.data = data;
51
+ }
52
+ }
53
+
54
+ */
55
+
56
+ public class Solution {
57
+
58
+ public static LinkedListNode<Integer> insert(LinkedListNode<Integer> head, int pos, int data){
59
+ //Your code goes here
60
+ //System.out.println("Insert at index: "+pos);
61
+ if (pos==0)
62
+ {
63
+ LinkedListNode<Integer> newNode=new LinkedListNode<>(data);
64
+ newNode.next=head;
65
+ head=newNode;
66
+ }
67
+ else
68
+ {
69
+ int i=0;
70
+ LinkedListNode<Integer> n=head;
71
+ for (i=0;i<pos-1 && n!=null;i++)
72
+ {
73
+ //System.out.println("At node: "+i);
74
+ n=n.next;
75
+ }
76
+ //System.out.println("At node: "+i);
77
+ if (n!=null && pos>0)
78
+ {
79
+ LinkedListNode<Integer> newNode=new LinkedListNode<>(data);
80
+ newNode.next=n.next;
81
+ n.next=newNode;
82
+ }
83
+ }
84
+
85
+ return head;
86
+ }
87
+ }
You can’t perform that action at this time.
0 commit comments