Skip to content

Commit be06d25

Browse files
authored
Update Delete Node in LL
1 parent 81a55b7 commit be06d25

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Course 2 - Data Structures in JAVA/Lecture 7 - Linked Lists I/Delete Node in LL

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,54 @@ Sample Output 2 :
3939
4 5 2 6 1 9
4040
10 20 30 40 50 60
4141
*/
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+
}

0 commit comments

Comments
 (0)