Delete All Even Nodes from a Singly Linked List in Java



In this article, we will learn to delete all even nodes from a singly linked list in Java. This Java program demonstrates how to create and manage a singly linked list, including adding nodes, deleting nodes with even values, and printing the list. You will see how to insert nodes, remove nodes with even values, and display the remaining nodes.

A singly linked list consists of nodes where each node has two parts: one part stores the data, and the other part holds the address of the next node. This setup allows traversal in only one direction, as each node is linked to the next node through a single connection.


Problem Statement

Write a program in Java to delete all even nodes from a singly linked list

Input

Original List:
1 2 3 4 5 6

Output

Original List:
1 2 3 4 5 6
List after deleting even nodes:
1 3 5

Steps to delete all even nodes from a singly linked list

Following are the steps to delete all even nodes from a singly linked list:

  • First we will start by creating the linked list we will initialize an empty linked list.
  • Add nodes with integer values (1, 2, 3, 4, 5, 6) to the end of the list.
  • Remove even nodes we will remove leading evens we check and remove even nodes from the beginning of the list.
  • Remove internal evens by traversing through the remaining list and removing nodes with even values.
  • Print the values of the nodes that remain in the list after removal.

Java program to delete all even nodes from a singly Linked List

Here's a Java program to delete all even nodes from a singly linked list:

public class LinkedList {
    // Node class
    static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    Node head;

    // Function to insert a node at the end of the list
    public void insert(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }

    // Function to delete all even nodes
    public void deleteEvenNodes() {
        if (head == null) return;

        // Removing even nodes at the beginning
        while (head != null && head.data % 2 == 0) {
            head = head.next;
        }

        // Remove even nodes that are not at the head
        Node current = head;
        while (current != null && current.next != null) {
            if (current.next.data % 2 == 0) {
                current.next = current.next.next;
            } else {
                current = current.next;
            }
        }
    }

    // Function to print the linked list
    public void printList() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(1);
        list.insert(2);
        list.insert(3);
        list.insert(4);
        list.insert(5);
        list.insert(6);

        System.out.println("Original List:");
        list.printList();

        list.deleteEvenNodes();

        System.out.println("List after deleting even nodes:");
        list.printList();
    }
}

Output

Original List:
1 2 3 4 5 6 
List after deleting even nodes:
1 3 5 

Code Explanation

The LinkedList class defines a linked list with methods to insert nodes, delete nodes with even values, and print the list. The Node class represents each node in the list, containing integer data and a reference to the next node. The insert() method adds new nodes to the end of the list. The deleteEvenNodes() method first removes even nodes from the beginning, then iterates through the list to remove any remaining even nodes. Finally, the printList() method outputs the values of the nodes in the list. The main method demonstrates these operations by creating a list, inserting values, removing even numbers, and printing the results.

Updated on: 2024-09-18T21:54:29+05:30

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements