-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList_Problem.java
More file actions
66 lines (63 loc) · 1.09 KB
/
LinkedList_Problem.java
File metadata and controls
66 lines (63 loc) · 1.09 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
62
63
64
65
66
package Daily_Problem;
import java.util.Stack;
public class LinkedList_Problem {
class Node{
int data;
Node next;
public void reverse() {
// TODO Auto-generated method stub
}
}
public Node head;
public static Node tail;
public int size=0;
public void addlast(int data)
{
Node nn=new Node();
nn.data=data;
nn.next=null;
if(size==0)
{
this.head=nn;
this.tail=nn;
this.size++;
}
else
{
this.tail.next=nn;
this.tail=nn;
size++;
}
}
public void display() {
Node temp=this.head;
while(temp!=null)
{
System.out.println(temp.data);
temp=temp.next;
}
}
public void reverse()
{
Node temp=this.head;
Stack<Integer> st=new Stack<>();
while(temp!=null)
{
st.push(temp.data);
temp=temp.next;
}
while(!st.isEmpty())
{
System.out.print(st.pop()+" ");
}
}
public static void main(String[] args) {
LinkedList_Problem obj=new LinkedList_Problem();
obj.addlast(1);
obj.addlast(2);
obj.addlast(3);
obj.addlast(4);
obj.display();
obj.reverse();
}
}