-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathReverse.java
85 lines (73 loc) · 2.2 KB
/
Reverse.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
73
74
75
76
77
78
79
80
81
82
83
84
85
// Given pointer to the head node of a linked list, the task is to reverse the linked list.
// We need to reverse the list by changing links between nodes.
// Examples:
// Input : Head of following linked list
// 1->2->3->4->NULL
// Output : Linked list should be changed to,
// 4->3->2->1->NULL
// Input : Head of following linked list
// 1->2->3->4->5->NULL
// Output : Linked list should be changed to,
// 5->4->3->2->1->NULL
// Input : NULL
// Output : NULL
// Input : 1->NULL
// Output : 1->NULL
// Algorithm - iterative
// 1. Initialize three pointers prev as NULL, curr as head and next as NULL.
// 2. Iterate trough the linked list. In loop, do following.
// Before changing next of current,
// store next node
// next = curr->next
// Now change next of current
// This is where actual reversing happens
// curr->next = prev
// Move prev and curr one step forward
// prev = curr
// curr = next
// Time Complexity: O(n)
// Space Complexity: O(1)
class LinkedList{
static Node head;
static class Node{
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}
public void print(Node node){ //pass the changed head as a parameter.
while(node != null){
System.out.print(node.data + " ");
node = node.next;
}
System.out.println("null");
}
Node reverse(Node node){
Node prev = null;
Node current = node;
Node next = null;
while(current != null){
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
public static void main(String args[]){
LinkedList llist = new LinkedList();
llist.head = new Node(1);
llist.head.next = new Node(2);
llist.head.next.next = new Node(3);
llist.head.next.next.next = new Node(4);
llist.head.next.next.next.next = new Node(5);
System.out.println("Original list - ");
llist.print(head);
head = llist.reverse(head);
System.out.println("Reversed list - ");
llist.print(head);
}
}