-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseAlternate.java
72 lines (72 loc) · 1.42 KB
/
ReverseAlternate.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.*;
public class ReverseAlternate {
static class Node{
int data;
Node next;
Node(int d,Node ptr){
data=d;
next=ptr;
}
}
static Node head=null;
public static Node insert(Node head,int data) {
if(head==null) {
return new Node(data,null);
}
else {
Node temp=head;
while(temp.next!=null) {
temp=temp.next;
}
temp.next=new Node(data,null);
return head;
}
}
public static void PrintLL(Node head) {
Node temp=head;
while(temp!=null) {
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
public static Node AlternateReverse(Node head) {
if(head==null) {
return null;
}
else {
Stack<Node> stack=new Stack<Node>();
Node temp=head;
while(temp!=null) {
stack.push(temp);
temp=temp.next;
}
temp=head;
Node temp2;
Node head2=null;
while(temp!=null) {
temp2=stack.pop();
head2=insert(head2,temp.data);
head2=temp==temp2?head2:insert(head2,temp2.data);
temp2.next=null;
temp=temp.next==temp2?null:temp.next;
}
head=head2;
return head;
}
}
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int data;
for(int i=0;i<n;i++) {
data=sc.nextInt();
head=insert(head,data);
}
PrintLL(head);
head=AlternateReverse(head);
PrintLL(head);
sc.close();
}
}