Skip to content

Commit de3447d

Browse files
authored
Update Delete Node Recursively
1 parent 2d13aed commit de3447d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Course 2 - Data Structures in JAVA/Lecture 8 - Linked Lists II/Delete Node Recursively

+38
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,41 @@ Sample Input 2 :
3737
Sample Output 2 :
3838
10 20 30 50
3939
*/
40+
/*
41+
42+
Following is the Node class already written for the Linked List
43+
44+
class LinkedListNode<T> {
45+
T data;
46+
LinkedListNode<T> next;
47+
48+
public LinkedListNode(T data) {
49+
this.data = data;
50+
}
51+
}
52+
53+
*/
54+
55+
public class Solution {
56+
57+
public static LinkedListNode<Integer> deleteNodeRec(LinkedListNode<Integer> head, int pos) {
58+
//Your code goes here
59+
if (head == null)
60+
{
61+
return head;
62+
}
63+
64+
if (pos==0)
65+
{
66+
head=head.next;
67+
return head;
68+
}
69+
else
70+
{
71+
LinkedListNode<Integer> smallerHead=deleteNodeRec(head.next,pos-1);
72+
head.next=smallerHead;
73+
return head;
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)