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