Skip to content

Commit 86ffa58

Browse files
authored
Update Reverse LL (Recursively)
1 parent de3447d commit 86ffa58

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Course 2 - Data Structures in JAVA/Lecture 8 - Linked Lists II/Reverse LL (Recursively)

+36
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,39 @@ Sample Output 2 :
3333
10
3434
50 40 30 20 10
3535
*/
36+
/*
37+
38+
Following is the Node class already written for the Linked List
39+
40+
class LinkedListNode<T> {
41+
T data;
42+
LinkedListNode<T> next;
43+
44+
public LinkedListNode(T data) {
45+
this.data = data;
46+
}
47+
}
48+
49+
*/
50+
51+
public class Solution {
52+
53+
public static LinkedListNode<Integer> reverseLinkedListRec(LinkedListNode<Integer> head) {
54+
//Your code goes here
55+
56+
if (head==null || head.next==null)
57+
{
58+
return head;
59+
}
60+
LinkedListNode<Integer> smallerHead=reverseLinkedListRec(head.next);
61+
LinkedListNode<Integer> node=smallerHead;
62+
while (node.next!=null)
63+
{
64+
node=node.next;
65+
}
66+
node.next=head;
67+
head.next=null;
68+
return smallerHead;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)