Created
February 21, 2021 13:36
-
-
Save akashkumarcs19/61e754c746788b7c8e1fa5304b490fbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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