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 8693813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:37:46+00:00 2026-06-13T00:37:46+00:00

My problem: My delete node method works fine for deleting any specified node from

  • 0

My problem: My delete node method works fine for deleting any specified node from a user created list except for the first element. How do I get this method to be able to delete the front of a list?

public void deleteNode(node spot, node front) {
    node current = spot, previous = front;

    while(previous.next != current) {
        previous = previous.next;
    }
    previous.next = current.next;
}

This is the full program code.

import java.io.*;

public class LinkedList {
public int num;
public node front;

//set front to null
public void init() {
    front = null;
}

//make a new node
public node makeNode(int num) {
    node newNode = new node();
    newNode.data = num;
    newNode.next = null;
    return newNode;
}

//find the end of a list
public node findTail(node front) {
    node current = front;

    while(current.next != null) {
        current = current.next;
    }
    return current;
}

//find a specified node
public node findSpot(node front, int num) {
    node current = front;
    boolean searching = true, found = false;

    while((searching)&&(!found)) {
        if(current == null) {
            searching = false;
        }
        else if(current.data == num) {
            found = true;
        }
        else {
            current = current.next;
        }
    }
    return current;
}

//delete a specified node
public void deleteNode(node spot, node front) {
    node current = spot, previous = front;

    while(previous.next != current) {
        previous = previous.next;
    }
    previous.next = current.next;
}

//add nodes to the end of a list
public void add2Back(node front, int num) {
    node tail;

    if (front == null) {
        front = makeNode(num);
    }
    else {
        tail = findTail(front);
        tail.next = makeNode(num);
    }
}

//add nodes after a specified node
public void addAfter(int num, node spot) {
    node newNode;
    newNode = makeNode(num);
    newNode.next = spot.next;
    spot.next = newNode;
}

//print out a list
public void showList(node front) {
    node current = front;

    while(current != null){
        System.out.println(current.data);
        current = current.next;
    }
}

public static void main(String [] args) throws IOException{
    //make a new list and node
    LinkedList newList = new LinkedList();
    node newNode = new node();
    //add data to the nodes in the list
    for(int j = 1; j < 10; j++){
        newList.add2Back(newNode, j);
    }
    //print out the list of nodes
    System.out.println("Auto-generated node list");
    newList.showList(newNode);

    //ask the user how many nodes to make, make those nodes, and show them
    System.out.println("Please enter how many nodes you would like made.");
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData = inputReader.readLine();
    int listLength = Integer.parseInt(inputData);
    LinkedList userList = new LinkedList();
    node userNode = new node();
    for(int j = 1; j < listLength; j++) {
        userList.add2Back(userNode, j);
    }
    userList.showList(userNode);

    //ask the user to add a new node to the list after a specified node
    System.out.println("Please enter a number for a node and then choose a spot from the list to add after.");
    BufferedReader inputReader2 = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData2 = inputReader2.readLine();
    BufferedReader inputReader3 = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData3 = inputReader3.readLine();
    int newNodeValue = Integer.parseInt(inputData2);
    int nodeInList = Integer.parseInt(inputData3);
    userList.addAfter(newNodeValue, userList.findSpot(userNode, nodeInList));
    userList.showList(userNode);

    //ask the user to delete a specified node
    System.out.println("Please enter a node to delete.");
    BufferedReader inputReader4 = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData4 = inputReader4.readLine();
    int nodeToDelete = Integer.parseInt(inputData4);
    userList.deleteNode(userList.findSpot(userNode, nodeToDelete), userNode);
    userList.showList(userNode);
}
}
  • 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-13T00:37:47+00:00Added an answer on June 13, 2026 at 12:37 am

    The problem is that your deleteNode does not modify the front member variable of your list, because the front variable inside deleteNode is a method parameter, not the instance variable front.

    Here is what you need to do:

    • Exposing front as a public member of the LinkedList is a violation of encapsulation. Make front a private variable.
    • Remove parameter front from all methods that take it; use the private member front instead.
    • Add a check in deleteNode to see if the spot to be deleted is the front. If it is, perform a special operation that assigns front a new value, and exit; otherwise, do the while loop that you already have.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is deleting a node from linked list. I have two structs :
Deleting a node from the middle of the heap can be done in O(lg
according to mdn documentation the method removeChild removes a node from the DOM but
EDIT: Problem: Deleting a node between 2 nodes, and then linking the outer nodes
I am New To C# I have problem... I want to delete selected node
I am having a problem with delete and destructor (I am sure I am
The problem i have is i could DELETE but then when i hit refresh
i have this problem: ms-access could not delete and i found a potential solution
I'm running into a problem with Hibernate where when trying to delete a group
I managed to found out how to delete field but I have a problem

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.