Knowee
Questions
Features
Study Tools

Write a Java function that delete odd nodes from the doubly linked list.

Question

Write a Java function that delete odd nodes from the doubly linked list.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java function that deletes odd nodes from a doubly linked list:

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

class LinkedList {
    Node head;

    void deleteOddNodes() {
        Node current = head;
        while (current != null) {
            if (current.data % 2 != 0) {
                if (current == head) {
                    head = current.next;
                    if (head != null)
                        head.prev = null;
                } else {
                    if (current.next != null)
                        current.next.prev = current.prev;
                    if (current.prev != null)
                        current.prev.next = current.next;
                }
            }
            current = current.next;
        }
    }
}

This function works by iterating through the linked list and checking if the data in the current node is odd. If it is, it deletes the node by adjusting the next and prev pointers of the adjacent nodes. If the node to be deleted is the head of the list, it updates the head to the next node.

This problem has been solved

Similar Questions

Given a doubly Linked list and a position. The task is to delete a node from a given position (position starts from 1) in a doubly linked list and return the head of the doubly Linked list.

Delete all nodes from a doubly linked list that are divisible by K

Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not given, can we delete the node X from given linked list?Possible if X is not last node. Use following two steps (a) Copy the data of next of X to X. (b) Delete next of X.Possible if size of linked list is even.Possible if size of linked list is oddPossible if X is not first node. Use following tw

What happens when you try to delete a node in a doubly linked list with only one node?The list becomes empty.The list becomes corrupted.The deletion fails.An error is thrown.

Write JAVA functions to implement DELETE_FIRST_NODE and TRAVERSEoperations in doubly linked lis

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.