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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:25:52+00:00 2026-05-30T16:25:52+00:00

If I have a variable LinkedList list and repeatedly do the following to extract

  • 0

If I have a variable

LinkedList list

and repeatedly do the following to extract the tail of ‘list’

// Some operation that adds elements to 'list'
// max_size = some constant
list = (LinkedList) list.subList(list.size()-max_size, list.size());

do I end up with a lot of references to the ‘previous’ list?

So basically what I’m trying to do here is to remove an initial segment of the list.

Is there a better way to remove an initial segment of a LinkedList? I think the data structure of LinkedList should allow linear time (linear in the size of the initial segment to be removed) operation.

  • 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-30T16:25:53+00:00Added an answer on May 30, 2026 at 4:25 pm

    That code will simply fail – the returned sublist is not a LinkedList<T>. Sample program:

    import java.util.LinkedList;
    
    public class Test {
        public static void main(String[] args) {
            LinkedList<String> list = new LinkedList<String>();
            list.add("x");
            list.add("y");
            list = (LinkedList<String>) list.subList(1, 2);
        }
    }
    

    Output:

    Exception in thread "main" java.lang.ClassCastException: java.util.SubList
        cannot be cast to java.util.LinkedList
        at Test.main(Test.java:8)
    

    It sounds like you should just be calling removeFirst as many times as you need to:

    while (list.size() > maxSize) {
        list.removeFirst();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a double link list that stores some information. When I try and
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I have a variable that is built in loop. Something like: $str = ;
I have a variable that contains a 4 byte, network-order IPv4 address (this was
I have a variable in code that can have file path or url as
I have a variable which is the head to a linked list. I want
I have an assignment that requires us to implement a doubly linked list class.
All, I have XML in the following format: <linked-list> <Description> <desc></desc> <IP></IP> </Description> </linked-list>
I have some trouble with pointers. I have a function that should return the
I have a text file that contains the following data. The first line is

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.