-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.java
More file actions
50 lines (41 loc) · 1.29 KB
/
Copy pathday3.java
File metadata and controls
50 lines (41 loc) · 1.29 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
import java.util.Stack;
public class ListNode {
int val;
ListNode next;
// Constructors
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
// Print the linked list starting from this node
public void printList() {
ListNode current = this;
while (current != null) {
System.out.print(current.val + " -> ");
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
// Create a linked list: 1 -> 2 -> 3 -> null
ListNode node3 = new ListNode(3);
ListNode node2 = new ListNode(2, node3);
ListNode head = new ListNode(1, node2);
// Print the list
head.printList(); // Output: 1 -> 2 -> 3 -> null
ListNode curr = head;
Stack<ListNode> rev = new Stack();
while (curr != null) {
rev.push(curr);
curr = curr.next;
}
ListNode newHead = rev.pop();
ListNode newCurr = newHead;
while(!rev.isEmpty()){
ListNode nextNode = rev.pop();
newCurr.next = nextNode;
newCurr = nextNode;
}
newCurr.next = null;
newHead.printList();
}
}