Skip to content

Instantly share code, notes, and snippets.

@sricharankrishnan
Last active March 24, 2023 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sricharankrishnan/4a0b27b036e65cff93485713e27e22aa to your computer and use it in GitHub Desktop.
Save sricharankrishnan/4a0b27b036e65cff93485713e27e22aa to your computer and use it in GitHub Desktop.
A singly linked list data structure implementation done in java along with respective time complexities for each method
/**
* Singly Linked List Implementation
*
* public Node get(int index): O(n)
* public void delete(E value): O(n)
* public boolean search(E value): O(n)
* public void insertAtPosition(E value, int index): O(n)
* public void insertAtTail(E value): O(n)
* public void insertAtHead(E value): O(1)
*/
public class SinglyLinkedList<E> {
private class Node<E> {
public E value;
public Node next;
public Node(E value) {
this.value = value;
this.next = null;
}
public String toString() {
return "[Node:" + this.value + "]";
}
}
private Node head;
private int size;
public SinglyLinkedList() {
this.head = null;
this.size = 0;
}
/* O(n) */
public Node get(int index) {
if (this.isEmpty()) {
return null;
}
else if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException("Index: " + index + " is out of bounds");
}
else {
Node node = null;
for (int i = 0; i <= index; ++i) {
node = (i == 0) ? this.head : node.next;
}
return node;
}
}
/* O(n) */
public void delete(E value) {
if (this.isEmpty() || Objects.isNull(value)) {
return;
}
/* if at head */
if ((E) this.head.value == value) {
this.head = this.head.next;
--this.size;
return;
}
/* otherwise */
Node current = this.head;
Node prev = null;
while (current != null && (E) current.value != value) {
prev = current;
current = current.next;
}
/* value not found in the singly linked list so stop */
if (Objects.isNull(current)) {
return;
}
/* otherwise, remove and update the singly linked list */
else {
prev.next = current.next;
--this.size;
}
}
/* O(n) */
public boolean search(E value) {
boolean result = false;
if (!this.isEmpty() && !Objects.isNull(value)) {
Node current = this.head;
while (current != null) {
if ((E) current.value == value) {
result = true;
break;
}
current = current.next;
}
}
return result;
}
/* O(n) */
public void insertAtPosition(E value, int index) {
if (!Objects.isNull(value)) {
if (this.isEmpty() || index == 0) {
this.insertAtHead(value);
return;
}
else if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException("Index: " + index + " is out of bounds");
}
else {
Node node = new Node(value);
Node prev = null;
Node next = null;
for (int i = 0; i < index; ++i) {
prev = (i == 0) ? this.head : prev.next;
}
next = prev.next;
prev.next = node;
node.next = next;
++this.size;
}
}
}
/* O(n) */
public void insertAtTail(E value) {
if (!Objects.isNull(value)) {
if (this.isEmpty()) {
this.insertAtHead(value);
return;
}
Node node = new Node(value);
Node current = this.head;
while (current.next != null) {
current = current.next;
}
current.next = node;
++this.size;
}
}
/* O(1) */
public void insertAtHead(E value) {
if (!Objects.isNull(value)) {
Node node = new Node(value);
node.next = this.head;
this.head = node;
++this.size;
}
}
public boolean isEmpty() {
return this.size == 0;
}
public int size() {
return this.size;
}
public String toString() {
String string = "";
Node current = this.head;
for (int i = 0; i < this.size; ++i) {
string += (i == this.size - 1) ? current : current + " -> ";
current = current.next;
}
return string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment