-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
149 lines (138 loc) · 3.83 KB
/
LinkedList.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package linear;
public class LinkedList{
Node head;
Node tail; // Additional reference to tail, this improved the insert operation from O(n) to O(1).
// Inserts new node at the end of the Linked List - O(1)
void insert(int value){
this.insertLast(value);
}
//O(1)
void insertLast(int value){
Node newItem = new Node(value);
if(head == null){
head = newItem;
tail = newItem;
} else{
tail.next = newItem;
tail = newItem;
}
}
//O(1)
void insertFirst(int value){
Node newItem = new Node(value);
if(head == null){
head = newItem;
tail = newItem;
} else{
newItem.next = head;
head = newItem;
}
}
//O(1)
boolean deleteFirst(){
if(head == null) return false; //Empty list
if(head == tail){ // single node
head = null;
tail = null;
}else{
head = head.next;
}
return true;
}
//O(n)
boolean deleteLast(){
if(head == null) return false; // Empty list
if(head == tail){ // single node
head = null;
tail = null;
}else{
Node current = head;
Node prev = null;
while(current.next != null){
prev = current;
current = current.next;
}
tail = prev;
prev.next = null;
}
return true;
}
// Iterates over the Linked List sequentially and returns the node if the item data equals node data. Returns null if not found.
//O(n)
Node find(int data){
if(head == null) return null;
Node current = head;
while(current != null){
if(current.data == data) return current;
current = current.next;
}
return null;
}
// Returns true if the delete is successfull else false.
//O(n)
boolean delete(int data){
if(head == null) return false;
Node current = head;
Node prev = null;
while(current != null){
if(current.data == data){
if(prev !=null){
prev.next = current.next;
if(prev.next == null) tail = prev;// Update tail if last node is deleted
}else{
head = head.next;// Move head to the next node
if(head == null){
tail = null;// If list becomes empty
}
}
return true;// Deletion successful
}else{
prev = current;
current = current.next;
}
}
return false; // Data not found in the list
}
void print(){
Node current = head;
while(current != null){
System.out.print(current.data +" ");
current = current.next;
}
System.out.println();
System.out.println("Current Head "+head.data);
System.out.println("Current Tail "+tail.data);
}
void reverse(){
Node prev = null;
Node current = head;
Node oldHead = head;
while(current != null){
Node temp = current.next;
current.next = prev;
prev = current;
current = temp;
}
head = prev; // Update head to the new front of the list
tail = oldHead;
}
public static void main(String args[]){
LinkedList ll = new LinkedList();
ll.insertLast(1);
ll.insertLast(2);
ll.insertLast(3);
ll.insertLast(4);
ll.insertLast(5);
ll.print();
ll.reverse();
ll.print();
}
}
class Node{
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}