Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions linked list using java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.arpit.java2blog;

class Node {
public int data;
public Node next;

public void displayNodeData() {
System.out.println("{ " + data + " } ");
}
}

public class SinglyLinkedList {
private Node head;

public boolean isEmpty() {
return (head == null);
}

// used to insert a node at the start of linked list
public void insertFirst(int data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = head;
head = newNode;
}

// used to delete node from start of linked list
public Node deleteFirst() {
Node temp = head;
head = head.next;
return temp;
}

// Use to delete node after particular node
public void deleteAfter(Node after) {
Node temp = head;
while (temp.next != null && temp.data != after.data) {
temp = temp.next;
}
if (temp.next != null)
temp.next = temp.next.next;
}

// used to insert a node at the start of linked list
public void insertLast(int data) {
Node current = head;
while (current.next != null) {
current = current.next; // we'll loop until current.next is null
}
Node newNode = new Node();
newNode.data = data;
current.next = newNode;
}

// For printing Linked List
public void printLinkedList() {
System.out.println("Printing LinkedList (head --> last) ");
Node current = head;
while (current != null) {
current.displayNodeData();
current = current.next;
}
System.out.println();
}
}


package org.arpit.java2blog;

public class LinkedListMain {

public static void main(String args[])
{
SinglyLinkedList myLinkedlist = new SinglyLinkedList();
myLinkedlist.insertFirst(5);
myLinkedlist.insertFirst(6);
myLinkedlist.insertFirst(7);
myLinkedlist.insertFirst(1);
myLinkedlist.insertLast(2);
// Linked list will be
// 2 -> 1 -> 7 -> 6 -> 5
Node node=new Node();
node.data=1;
myLinkedlist.deleteAfter(node);
// After deleting node after 1,Linked list will be
// 2 -> 1 -> 6 -> 5
myLinkedlist.printLinkedList();
}
}