-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseLinkedList.java
More file actions
59 lines (50 loc) · 1.71 KB
/
Copy pathReverseLinkedList.java
File metadata and controls
59 lines (50 loc) · 1.71 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
class LinkedList {
// Static variable to store the head of the linked list
static Node head;
static class Node {
int data; // Data stored in the node
Node next_node; // Reference to the next node
Node(int d) {
data = d;
next_node = null;
}
}
// Function to reverse the linked list
Node reverse(Node node) {
Node prev_node = null;
Node current_node = node;
Node next_node = null;
while (current_node != null) {
next_node = current_node.next_node;
current_node.next_node = prev_node;
prev_node = current_node;
current_node = next_node;
}
node = prev_node;
return node;
}
// Function to print the elements of the linked list
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next_node;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Create a linked list with some initial values
list.head = new Node(20);
list.head.next_node = new Node(40);
list.head.next_node.next_node = new Node(60);
list.head.next_node.next_node.next_node = new Node(80);
// Print the original linked list
System.out.println("Original Linked list:");
list.printList(head);
// Reverse the linked list
head = list.reverse(head);
System.out.println("");
// Print the reversed linked list
System.out.println("Reversed Linked list:");
list.printList(head);
}
}