Skip to content

Instantly share code, notes, and snippets.

@Gkemon
Created July 7, 2021 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gkemon/b419e9f757668be56dbd619f705d2609 to your computer and use it in GitHub Desktop.
Save Gkemon/b419e9f757668be56dbd619f705d2609 to your computer and use it in GitHub Desktop.
LinkedList.java
public class TestLinkedListImplementation {
public static void main(String args[]) {
LinkedList ll = new LinkedList();
ll.addValue(1);
ll.addValue(2);
ll.addValue(3);
ll.addValue(4);
ll.addValue(5);
ll.addValue(6);
ll.printAll();
}
}
class LinkedList {
Node headNode;
public void addValue(int value){
if(headNode==null){
headNode = new Node(value);
}else {
headNode.setValueToTheNext(value);
}
}
public void printAll(){
headNode.print();
}
}
class Node{
Integer value=null;
Node nextNode=null;
public Node(int value){
this.value=value;
}
public void setValueToTheNext(int value){
if(nextNode==null){
nextNode=new Node(value);
}else nextNode.setValueToTheNext(value);
}
public void print(){
System.out.println("value: "+value+"\n\n");
if(nextNode!=null){
nextNode.print();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment