-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
59 lines (54 loc) · 1.68 KB
/
LinkedList.java
File metadata and controls
59 lines (54 loc) · 1.68 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
public class LinkedList {
private Node head;
//constructor to initialize the LinkedList head to null
public LinkedList(){
head = null;
}
/**
* Insertion Method
* @param newNode
*/
public void insertAfter(Node newNode){
if (head == null){
head = newNode; //if the list is empty, the new node will be the head
}
else {
Node current = head;
while (current.next != null){
current = current.next; //traverse to the end of the list
}
current.next = newNode; //insert the new node at the end of the list
}
}
/**
* method for removing a specific key
* @param key
*/
public void deletion(int key) {
if (head == null) {
return; //list is empty
}
else if(head.data == key) {
head = head.next;
return; //remove the head node
}
Node current = head; //to be able to traverse the list
while (current.next != null && current.next.data != key) {
current = current.next; //traverse the list
}
if (current.next != null && current.next.data == key) {
current.next = current.next.next; //removes the node to be deleted from the list
}
}
public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data);
if (current.next != null) {
System.out.print(" -> ");
}
current = current.next;
}
System.out.println(" -> null");
}
}