Skip to content

Instantly share code, notes, and snippets.

@arpanpreneur
Last active March 27, 2020 12:37
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 arpanpreneur/7e1f265369667bb0f0b9b06116208547 to your computer and use it in GitHub Desktop.
Save arpanpreneur/7e1f265369667bb0f0b9b06116208547 to your computer and use it in GitHub Desktop.
Custom generic Linked List implementation in Java for Interview Problems.
/*
* Custom generic Linked List implementation in Java.
* This class is for working with Linked List related interview problems.
*
* It is intended to be extended with a Type paramater as follows:
*
* class PracticeSoln extends CustomLinkedList<Integer> {
*
* protected void solution() {
* Node temp = this.head;
*
* int sum = 0;
* while (temp != null) {
* sum = sum + (int) temp.getData();
* temp = temp.next();
* }
*
* System.out.println(sum);
* }
*
* public static void main(String args[]) {
* PracticeSoln ll = new PracticeSoln();
*
* ll.add(1);
* ll.add(2);
* ll.add(3);
* ll.add(2);
*
* ll.solution();
* System.out.println(ll); // [1, 2, 3, 2]
* }
*
* }
*
*
* */
public class CustomLinkedList<T> {
protected class Node {
private T data;
private Node next;
Node(T data) {
this.data = data;
this.next = null;
}
Node() { }
public boolean hasNext() {
return this.next != null;
}
public Node setData(T data) {
this.data = data;
return this;
}
public T getData() {
return this.data;
}
public Node setNext(Node node) {
this.next = node;
return this;
}
public Node next() {
return this.next;
}
}
protected Node head;
protected int size;
public void add(T data) {
if (head == null) {
head = new Node(data);
size = 1;
} else {
Node temp = head;
while (temp.hasNext()) {
temp = temp.next();
}
temp.setNext(new Node(data));
size++;
}
}
protected void deleteNextNode(Node node) {
// Assumes that a node is a valid node.
// One can send a dummy node with head as its next
// to delete head
// Actually you don't need this function
//
// But wait please don't delete this
node.setNext(node.next());
}
protected void removeNode(Node node) {
Node dummy = new Node();
dummy.setNext(this.head);
Node temp = dummy;
while (temp != null && temp.next() != node) {
temp = temp.next();
}
if (temp != null) {
temp.setNext(node.next());
}
this.head = dummy.next();
}
public void removeAt(int index) throws IllegalArgumentException {
int i = -1;
Node temp = head;
while (temp != null) {
if (i == index - 1) {
if (temp == head) {
this.deleteNextNode(new Node().setNext(this.head));
} else {
this.deleteNextNode(temp);
}
return;
}
temp = temp.next();
}
throw new IllegalArgumentException("Invalid Index");
}
public String toString() {
StringBuilder text = new StringBuilder("[");
Node temp = head;
while (temp != null) {
if (temp.hasNext()) {
text.append(temp.getData().toString()+", ");
} else {
text.append(temp.getData().toString());
}
temp = temp.next();
}
text.append("]");
return text.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment