Question

// a function to delete the second node if exists and its value is odd, //...

// a function to delete the second node if exists and its value is odd,

// otherwise the list is unchanged

// Examples

// {0,1,2,8,9}.deleteSecondIfOdd() --> {0,2,8,9}

// {0,9}.deleteSecondIfOdd() --> {0}

// {0,4,9}.deleteSecondIfOdd() --> {0,4,9}

// {}.deleteSecondIfOdd() --> {}

public void deleteSecondIfOdd () {

// TODO 4: fix this

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

Here we considered head as first node of list

public void deleteSecondIfOdd () {
  
   Node first = head;
   if(first == null)return;//if list is empty then return
   Node second = head.next;

   //if list has only one node or second element is even then return
   if(second == null || second.data%2 ==0)return;
   first.next = second.next; //deleting second node
}

Add a comment
Know the answer?
Add Answer to:
// a function to delete the second node if exists and its value is odd, //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void...

    implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void delNode (DLList list, char *str) ( // Delete node containing word from list if it is present void delNode (DLList list, char *str) (

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node...

    C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...

  • I don't know how to do the clear function, which mean delete the nodes between n1 and n2 # include<ios-ream>...

    I don't know how to do the clear function, which mean delete the nodes between n1 and n2 # include<ios-ream> using namespace sta; General instructions: Please modify this file and submit the modified one via svn and then websubmission (under the entry "pracExam"). No design documents are needed. struct Node int data; Node* next; /Task 1: Implement the following function for adding a front of the given list. new node to the input Node head: a head pointer to a...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • inorderDraw(): This function assigns x coordinates to nodes so that each node gets a distinct x-coordinate,...

    inorderDraw(): This function assigns x coordinates to nodes so that each node gets a distinct x-coordinate, and each node receives an x-coordinate between that of its two children. _____________________________________ package comp2402a4; import java.util.Random; public class GeometricTree extends BinaryTree {    public GeometricTree() {        super (new GeometricTreeNode());    }       /**    * Set the x and y-coordinates of each node such that it is between the    * x-coordinate of its two children, no two nodes have...

  • This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node...

    This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node *head) if (head == NULL)    return head;    if (head->data % 2 == 0) { node *temp = head; free(temp); return deleteEven(head->next); } else { head->next = deleteEven(head->next); return head; } } What is the purpose of heaving to set "head->next = deleteEven(head->next);" instead of just having "head = head->next;" in one of the lines above and just calling "deleteEven(head->next);"?

  • #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left;...

    #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left; // left child Node* right; // right child Node* p; // parent Node(int data) { value = data; left = NULL; right = NULL; p = NULL; } ~Node() { } int d() { return value; } void print() { std::cout << value << std::endl; } }; int main(int argc, const char * argv[]) { } function insert(Node *insert_node, Node *tree_root){ //Your code here...

  • C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...

    C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT