-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedLists.java
180 lines (141 loc) · 3.41 KB
/
LinkedLists.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
class Node{
int val;
Node next;
Node(){
val = 0;
next = null;
}
Node(int v, Node next){
val = v;
this.next = next;
}
}
class LinkedListsMethods{
/*
1. Add element to end
2. Delete element from end
3. Add element to front
4. Delete element from front
5. Traverse a linked list
*/
private Node head;
LinkedListsMethods(){
head = null;
}
Node getHead(){
return head;
}
void addAtEnd(int val){
if(head == null){
head = new Node(val, null);
return;
}
Node curr = head;
while(curr.next != null){
curr = curr.next;
}
curr.next = new Node(val, null);
}
void traverse(){
System.out.println("Elements in the linkedlist:");
Node curr = head;
while(curr != null){
System.out.print(curr.val);
if(curr.next != null)
System.out.print("--->");
curr = curr.next;
}
System.out.println("");
}
void addAtFront(int val){
if(head == null){
head = new Node(val, null);
return;
}
/*
head ->50 -> 10 -> 20 -> 30
head = 2000
addAtFront(50);
[50, 1000] -> 2000
temp = 2000
*/
Node temp = new Node(val, head);
head = temp;
}
void deleteAtIndex(int i){
Node curr = head.next;
Node prev = head;
if(i == 0){
head = head.next;
return;
}
while(curr != null && (i-- > 0)){
prev = curr;
curr = curr.next;
}
if(curr == null){
return;
}
prev.next = curr.next;
}
void reverseLinkedList(){
//Homework
/*
null<-1<-2<-3<-4<-head
curr = null
prev = 4
next = null
*/
Node curr, prev;
curr = head.next;
if(curr == null) return;
prev = head;
while(curr != null){
Node next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head.next = null;
head = prev;
}
void swap(int k1){
Node curr = head;
Node prev = null;
while(curr.val != k1){
prev = curr;
curr = curr.next;
}
if(prev == null){
//head->2->1->3 => 2->1->3 temp = 3
Node temp = curr.next.next;
curr.next.next = curr;
head = curr.next;
curr.next = temp;
} else{
//1-->3 ->2 -> 4 -> 1->3->2->4 curr = 2, prev = 1 temp=4
prev.next = curr.next;
Node temp = curr.next.next;
curr.next.next = curr;
curr.next = temp;
}
}
}
public class LinkedLists {
public static void main(String[] args) {
LinkedListsMethods lmethods = new LinkedListsMethods();
lmethods.addAtEnd(10);
lmethods.addAtEnd(20);
lmethods.addAtEnd(30);
lmethods.addAtFront(50);
lmethods.deleteAtIndex(1);
lmethods.traverse();
}
}
/*
[200, 300, 100, 20, 10]
1000 1004 1008 1012 1016
arr[i] = base_loc + i*4
[200, 300, 100, 20, 10]
[1000, 2000, 500, 10000, 20
*/