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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:15:34+00:00 2026-05-13T10:15:34+00:00

I am trying to implement the linked list data structure using java, it works

  • 0

I am trying to implement the linked list data structure using java, it works fine for insertion or removing first elements but fails to remove the last element via using the removeLast() method.

My Linked List Node class:
public class LLNode {
String value;
LLNode next;

    public LLNode(String value){
        this.value = value;
        this.next = null;
    }

}

My Linked List class that holds a head Node:

public class LL {
    LLNode head;

    public LL(){
        this.head = null;   
    }

    public void insertHead(LLNode input){
        input.next = head;
        this.head = input;
    }

    public void removeFirst(){
        this.head = this.head.next;
    }

    public void removeLast(){
        if (this.head == null || this.head.next == null){
            this.head = null;
            return;
        }
        LLNode current = this.head;
        LLNode tmphead = current;
        while(current.next.next != null){
            current = current.next;
        }
        current.next.next = null;
        this.head = tmphead ;
    }

    public void printAll(){
        LLNode current = this.head;

        while(current != null){
            System.out.print(current.value+" ");
            current = current.next;
        }
        System.out.println();
    }


    public static void main( String[] args){

        LL test = new LL(); 
        LL test2 = new LL();
        String[] eben = {"one","two","three","four","five","six"};

        for(int i =0;i<eben.length;i++){
            test.insertHead(new LLNode(eben[i]));
        }
        test.printAll();
        test.removeFirst();
        test.printAll();
        test.removeLast();
        test.printAll();


    }

}

The output is such:

six five four three two one 
five four three two one 
five four three two one 

even tho it had to be like this:

six five four three two one 
five four three two one 
five four three two

What is wrong with my implementation ?

  • 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-05-13T10:15:35+00:00Added an answer on May 13, 2026 at 10:15 am

    If you need a lot of code and/or a lot of variables for a task like this, you’ve probably overcomplicated yourself into a corner. Here’s how I would do removeLast():

    public void removeLast() {
      // avoid special case: List is empty
      if (this.head != null) {
        if (this.head.next == null) {
          // handle special case: List has only 1 node
          this.head = null;
        } else {
          LLNode prelast = this.head; // points at node before last (eventually)
          while (prelast.next.next != null) prelast = prelast.next;
          prelast.next = null;
        }
      }
    }
    

    Untested! Use at your own risk. No refunds of purchase price.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can try using select() just as a timer. I… May 16, 2026 at 5:24 pm
  • Editorial Team
    Editorial Team added an answer You just need to override onCreateDialog in an Activity. //In… May 16, 2026 at 5:24 pm
  • Editorial Team
    Editorial Team added an answer Make use of the PHP's built-in function shuffle to do… May 16, 2026 at 5:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have been trying to implement XOR linked list and its operations but I
I'm trying to create a linked list, I've got it working, but I'm still
Here is code in which I am trying to implement a queue using linked
I'm trying to develop a simple IRC bot. First I want to think out
I am trying to implement an Edit ViewModel for my Linq2SQL entity called Product.
I'm trying to find a way to implement file manager functionality in an mvc
Trying to implement the following behavior on an iPad. I have a map-centric application
Im trying to implement a single aspx page with several externals dll's. Following Zimmergren's
Im trying to implement multi-language support in my system, the other systems at work
I'm trying to implement a B-Tree according to the chapter B-Trees in Introduction to

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.