-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
61 lines (61 loc) · 1.62 KB
/
DoublyLinkedList.java
File metadata and controls
61 lines (61 loc) · 1.62 KB
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
57
58
59
60
61
import java.util.Scanner;
public class DoublyLinkedList{
static class Node{
int data;
Node left,right;
Node(int data,Node left,Node right){
this.data=data;
this.left=left;
this.right=right;
}
}
static Node head=null;
public static Node insertNode(Node head,int data){
if(head==null){
return new Node(data,null,null);
}
else{
Node temp=new Node(data,null,null);
temp.right=head;
head.left=temp;
head=temp;
return head;
}
}
public static void displayLinkedList(Node head){
Node temp=head;
while(temp!=null){
System.out.println(temp.data);
temp=temp.right;
}
}
public static Node reverseNode(Node head){
if(head==null){
return null;
}
else{
//Node a,b;
Node temp=head;
Node head2=null;
while(temp!=null){
head2=insertNode(head2, temp.data);
temp=temp.right;
}
return head2;
}
}
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(int i=0;i<n;i++){
int data=sc.nextInt();
head=insertNode(head, data);
}
displayLinkedList(head);
head=reverseNode(head);
System.out.println("Reverse List is: ");
displayLinkedList(head);
sc.close();
}
}