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
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
ADVERTISEMENT