-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
executable file
·176 lines (137 loc) · 5.22 KB
/
Copy pathLinkedList.java
File metadata and controls
executable file
·176 lines (137 loc) · 5.22 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
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
package com.codeWithArsalon.LinearDS;
import java.util.NoSuchElementException;
// addFirst - O(1)
// addLast - O(1)
// removeFirst - O(1) - update head to point to next item
// RemoveLast - O(n)
// contains - O(n) - traverses entire list to look for item (worst case))
// indexOf - O(n) - traverses entire list to look for item (worst case))
//toArray - O(n) - traverse entire list of nodes, storing into an array
//getPrevious - O(n) - traverse entire list
//reverse - O(n) -
public class LinkedList {
private Node first;
private Node last;
private int size;
private class Node {
private int value;
private Node next;
public Node(int value) { //constructor for the node class
this.value = value;
}
}
public void addFirst(int item) {
//O(1) operation, just update tail's direction
var node = new Node(item); //create a new node object, set .value = item
if (isEmpty()) //sets first and last to this new node
first = last = node;
else {
node.next = first; //otherwise prepend to list (set head)
first = node;
}
size ++;
}
public void addLast(int item) {
//O(1) operation - because don't have to traverse the list of nodes
var node = new Node(item); //creating a new Node with the value set to item passed in as argument
if (isEmpty()) //if list is empty, sets first and last to this node
first = last = node;
else {
last.next = node; //add node after last node
last = node; //update last to point to this new node in the list
}
size ++;
}
public void removeFirst(){
if(isEmpty())
throw new NoSuchElementException();
if(first == last) //means we have a single node (item) in list
first = last = null; //set single node to null, don't execute following logic
else {
var second = first.next; //store the reference to the second node + other node (chain) in the list
first.next = null; //removes link to other nodes
first = second; //sets second node to first
}
size--;
}
public void removeLast(){
if(isEmpty())
throw new NoSuchElementException();
if(first == last) //means single node in list
first = last = null; //set the single node to null
else{
var previous = getPrevious(last); //O(n) operation
last = previous; //shrinks our list (makes tail second to last node)
last.next = null; //cuts the connection with the end node
}
size --;
}
public int indexOf(int item) {
int index = 0; //declare a variable to keep track index = 0 ; increment index
var current = first;
while (current != null) { //means haven't reached end of our list
if (current.value == item)
return index; //iterate over the list comparing index of current node with parameter index
current = current.next; //move current variable reference to next node in list
index++;
}
return -1;
}
public boolean contains(int item) {
return indexOf(item) != -1; //if this evaluates true, we have item in list, returns true
}
private Node getPrevious(Node node) {
var current = first;
while (current != null){
if(current.next == node) return current; //if the next node is our value, we got previous node, return current
current = current.next; //else, update pointer to next node in the list
}
return null; //if the node isn't found
}
private boolean isEmpty() {
return first == null;
}
public int size (){
return size;
}
public int [] toArray(){
int [] array = new int[size];
var current = first;
var index = 0;
while(current != null){
array[index++] = current.value;
current = current.next;
}
return array;
}
public void reverse(){
if(isEmpty()) return;
var previous = first;
var current = first.next;
while(current != null){
var next = current.next; //set point a
current.next = previous;//reverse link
previous = current; //move point a
current = next; //move point b
}
last = first; //sets the last node
last.next = null; //de-links old reference of the final node
first = previous;
}
public int getKthFromEnd(int k){
if(isEmpty())
throw new IllegalStateException();
var a = first;
var b = first;
for(int i = 0; i < k - 1; i++) { //setting point a kth distance behind b
b = b.next;
if (b == null) //moving b to an invalid k value parameter
throw new IllegalArgumentException();
}
while(b != last) {
b = b.next; //move point b forward until end of list
a = a.next; //move point a forward until b is at end of list
}
return a.value; //return value of kth node at position a
}
}