-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist.java
56 lines (49 loc) · 1.21 KB
/
linklist.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class linklist {
Node head;
class Node{
String data;
Node next;
Node(String data){
this.data=data;
this.next=null;
}
}
public void buildlist(String data){
Node nn=new Node(data);
if(head==null) {
head=nn;
return;}
nn.next=head;
head=nn;
}
public void print(){
if(head==null){
System.out.println("List is empty");
}
Node cn=head;
while(cn!=null){
System.out.print(cn.data + "->");
cn=cn.next;
}
System.out.println("NULL");
}
public Node reverseRecur(Node head){
if(head==null || head.next==null){
return head;
}
Node nn=reverseRecur(head.next);
head.next.next=head;
head.next=null;
return nn;
}
public static void main(String[] args) {
linklist list=new linklist();
list.buildlist("linkedlist");
list.buildlist("is");
list.buildlist("this");
list.buildlist("hey");
list.print();
list.head=list.reverseRecur(list.head);
list.print();
}
}