-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
68 lines (54 loc) · 1.71 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
public class LinkedList {
private Node head = new Node(null, 0);
public Node getHead() {
return head;
}
//returns the length of the list
public int size(){
int length = 0;
while (this.head.getNext() != null){
this.head = this.head.getNext();
length++;
}
return length;
}
public void make_Empty(){
Node temp = this.head;
temp.setNext(null);
}
public String turn_into_string(){
Node finallistcur = this.getHead().getNext();
StringBuilder sb = new StringBuilder();
while (finallistcur != null){
sb.insert(0, finallistcur.getKey());
finallistcur = finallistcur.getNext();
}
String final_string = sb.toString();
return final_string;
}
//returns true if list is empty, meaning head.getNext() == null
public boolean isEmpty(){
if (head.getNext() == null){
return true;
}
return false;
}
//adds a new node with the inputted value
//if list is empty meaning head.getNext() == null then head.next is set to new node
/*if list not empty, uses while loop to traverse list be continuously checking next value for a null.
Once a null is found for the next node, the node current is set to the new node for its next value
*/
public void add(int value){
Node node = new Node(null, value);
if (this.isEmpty()){
this.head.setNext(node);
}
else {
Node finalspot = this.head;
while (finalspot.getNext() != null){
finalspot = finalspot.getNext();
}
finalspot.setNext(node);
}
}
}