Skip to content

Instantly share code, notes, and snippets.

@akashkumarcs19
Created February 21, 2021 13:36
Show Gist options
  • Save akashkumarcs19/61e754c746788b7c8e1fa5304b490fbe to your computer and use it in GitHub Desktop.
Save akashkumarcs19/61e754c746788b7c8e1fa5304b490fbe to your computer and use it in GitHub Desktop.
class LinkedList{
static Node head;
static class Node{
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
//if the list is empty;
public static void push(int Data) {
Node temp = head;
Node new_node = new Node(Data);
if (head == null) {
head = new_node;
head.next = null;
} else {
while (temp.next != null) {
temp = temp.next;
}
temp.next = new_node;
new_node.next=null;
}
}
public static void printList(){
Node current = head;
while(current!=null){
System.out.println(current.data+" ");
current=current.next;
}
}
public static void main(String[] args) {
LinkedList n = new LinkedList();
push(56);
push(15);
push(485);
printList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment