How to implement singly linked list in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravipankaj
    New Member
    • Apr 2021
    • 5

    #1

    How to implement singly linked list in Java

    package com.ds.algorith ms.linkedlist.S LLcustomImpleme nt;
    /** * @Author pankaj * @create 4/11/21 6:19 PM */
    public class Node {
    int data;
    Node next;
    }
    — — — — — — — — — — — — — — — — — — — — — — — — — — — —
    package com.ds.algorith ms.linkedlist.S LLcustomImpleme nt;
    /**
    * @Author pankaj
    * @create 4/11/21 6:25 PM
    */
    public class LinkedList {
    Node head;// first Node
    public void add(int data) {
    // create Node before add it to LL
    Node node=new Node();
    node.data=data;
    node.next=null;
    // if you are adding first node, List is empty
    if (head == null)
    {
    head=node;
    }
    // if head(first node is already added
    else {
    Node travers=head;// to traverse Node, to hold temp data
    while (travers.next!= null)
    {
    travers=travers .next;// going to next node
    }
    // after reaching to last node
    travers.next=no de;// newly created Node is pointing
    }
    }
    // add at first
    public void addAtFirst(int data)
    {
    Node node=new Node();
    node.data=data;
    node.next=null;
    node.next=head; // newly created next value will be previous head node
    // change newly created Node to Head
    head=node;
    }
    // addAt index
    public void addAtIndex(int index,int data)
    {
    Node node=new Node();
    node.data=data;
    node.next=null;
    // add Node to 0th index — .> add it to first Location
    if(index==0)
    {
    addAtFirst(data );
    }else {
    Node n = head;
    for (int i = 0; i < index — 1; i++) {
    n = n.next;
    }
    node.next = n.next;// change ref, index-1 Node to newly created Node
    n.next = node;
    }
    }
    // delete value
    public void deleteAt(int index)
    {
    if(index==0)
    {
    head=head.next;
    } else {
    Node n=head;
    Node nRef=null;
    for (int i=0;i<index-1;i++) // reach to that specific index
    {
    n=n.next;
    }
    // store ref of next Node of index element
    nRef=n.next;
    n.next=nRef.nex t;
    System.out.prin tln(“ Deleted : INDEXED “+ index+” DATA “+nRef.data);
    }
    }
    // display method
    public void display()
    {
    Node node=head;
    while (node.next !=null)
    {
    System.out.prin t(node.data+” -> “);
    node=node.next;// going to next node
    }
    System.out.prin t(node.data); // print last Node
    }
    }
    — — — — — — — — — — — — — — — — — — — — — — — — — -
    package com.ds.algorith ms.linkedlist.S LLcustomImpleme nt;
    /**
    * @Author pankaj
    * @create 4/11/21 6:21 PM
    */
    public class RunLinkedList {
    public static void main(String[] args) {
    LinkedList linkedList=new LinkedList();
    linkedList.add( 10);
    linkedList.add( 45);
    linkedList.add( 78);
    linkedList.addA tFirst(520);
    linkedList.addA tIndex(3,300);
    linkedList.addA tIndex(0,100);
    linkedList.disp lay();
    linkedList.dele teAt(1);
    linkedList.disp lay();
    }
    }
Working...