-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathStackUsingLinkedList.java
More file actions
52 lines (36 loc) · 775 Bytes
/
Copy pathStackUsingLinkedList.java
File metadata and controls
52 lines (36 loc) · 775 Bytes
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
package util;
public class StackUsingLinkedList {
ListNode head;
public StackUsingLinkedList() {
head = null;
}
void push(int x) {
// Create a new node
ListNode node = new ListNode(x);
node.next = head;
// Move the head
head = node;
}
int pop() {
int number = head.val;
// Actually pop
head = head.next;
return number;
}
int peek() {
return head.val;
}
boolean isEmpty() {
return head == null;
}
public static void main(String[] args) {
StackUsingLinkedList myStack = new StackUsingLinkedList();
myStack.push(4);
myStack.push(8);
System.out.println(myStack.peek());
myStack.push(15);
myStack.pop();
myStack.pop();
System.out.println(myStack.pop());
}
}