Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:32:51+00:00 2026-06-13T19:32:51+00:00

I am trying to implement the deletion function in public static boolean delete(Object d,

  • 0

I am trying to implement the deletion function in

public static boolean delete(Object d, ListElement head){
ListElement p=find(d,head);
if(p.data==null){return false;}
else {
    System.out.println("Delete Successfully!");
    if(head.data==d){head=head.next;}
    else{
    while(head.next.data!=d){
        head=head.next;
    }
    head.next=head.next.next;}
    return true;}

}

This function basically check if the element d is in the list,
-if not->return false;

-else check whether the element is the first element of the list, if true, change the head to its next,

-else traverse to the list element in front of it.

The problem is case the element to delete is the first element, such as boolean s=ListElement.delete(1,d); I cannot use “head=head.next;” to assign new value to head. But java is passed by reference, why cannot I change that?

//actually I found my question is whether we can change the reference passed to the function inside the function
like:

    void fun(dog a){
    a=new dog("Po");
}
main()   
{dog b=new dog("Jo");fun(b);}

//so will b be changed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:32:52+00:00Added an answer on June 13, 2026 at 7:32 pm

    The reference to the first list element is either held by the list object itself or by an “invisible” root element (in case of single linked list).

    So you either have to pass the entire list to the method or, if you have that invisible root, pass the root as head.

    public static boolean delete(Object d, MyLinkedList<ListElement> list) {
    
      ListElement head = list.getHead();
      if (head.data.equals(d)) {   // <- ALWAYS use equals, never == to compare objects!!
        list.setHead(head.next);
      } else {
        ListElement element = head.next;
    
        // ... the rest is similiar to your algorithm
    
      } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement recursive deletion of Registry Keys for both 32-bit and
Guys I'm trying to implement deletion algorithm for a Red Black tree and I'm
I'm trying to implement a delayed blog post deletion scheme. So instead of an
I am trying to implement binary search tree operations and got stuck at deletion.
Ok well I'm trying implement something similar to the 'undo' function in many image
Trying to implement AVAudioplayer and get some metering data of the played music, but
I'm trying to implement undo support using Core Data on the iPhone and I
I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file
Trying to implement the decorator pattern in C# from the code in the Head
trying to implement the following but its not working <script> function loadmap(lat,lng) { //alert(lat,lang);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.