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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:01:00+00:00 2026-06-01T13:01:00+00:00

Having read this question Immutable or not immutable? and reading answers to my previous

  • 0

Having read this question Immutable or not immutable? and reading answers to my previous questions on immutability, I am still a bit puzzled about efficient implementation of simple LinkedList that is immutable. In terms of array tha seems to be easy – copy the array and return new structure based on that copy.

Supposedly we have a general class of Node:

class Node{
    private Object value;
    private Node next;
}

And class LinkedList based on the above allowing the user to add, remove etc. Now, how would we ensure immutability? Should we recursively copy all the references to the list when we insert an element?

I am also curious about answers in Immutable or not immutable? that mention cerain optimization leading to log(n) time and space with a help of a binary tree. Also, I read somewhere that adding an elem to the front is 0(1) as well. This puzzles me greatly, as if we don’t provide the copy of the references, then in reality we are modifying the same data structures in two different sources, which breaks immutability…

Would any of your answers alo work on doubly-linked lists? I look forward to any replies/pointers to any other questions/solution. Thanks in advance for your help.

  • 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-01T13:01:02+00:00Added an answer on June 1, 2026 at 1:01 pm

    Supposedly we have a general class of Node and class LinkedList based on the above allowing the user to add, remove etc. Now, how would we ensure immutability?

    You ensure immutability by making every field of the object readonly, and ensuring that every object referred to by one of those readonly fields is also an immutable object. If the fields are all readonly and only refer to other immutable data, then clearly the object will be immutable!

    Should we recursively copy all the references to the list when we insert an element?

    You could. The distinction you are getting at here is the difference between immutable and persistent. An immutable data structure cannot be changed. A persistent data structure takes advantage of the fact that a data structure is immutable in order to re-use its parts.

    A persistent immutable linked list is particularly easy:

    abstract class ImmutableList
    {
        public static readonly ImmutableList Empty = new EmptyList();
        private ImmutableList() {}
        public abstract int Head { get; }
        public abstract ImmutableList Tail { get; }
        public abstract bool IsEmpty { get; }
        public abstract ImmutableList Add(int head);
        private sealed class EmptyList : ImmutableList
        {
            public override int Head { get {  throw new Exception(); } }
            public override ImmutableList Tail { get { throw new Exception(); } }
            public override bool IsEmpty { get { return true; } }
            public override ImmutableList Add(int head)
            {
                return new List(head, this);
            }
        }
    
        private sealed class List : ImmutableList
        {
            private readonly int head;
            private readonly ImmutableList tail;
            public override int Head { get { return head; } }
            public override ImmutableList Tail { get { return tail; } }
            public override bool IsEmpty { get { return false; } }
            public override ImmutableList Add(int head)
            {
                return new List(head, this);
            }
        }
    }
    ...
    ImmutableList list1 = ImmutableList.Empty;
    ImmutableList list2 = list1.Add(100);
    ImmutableList list3 = list2.Add(400);
    

    And there you go. Of course you would want to add better exception handling and more methods, like IEnumerable<int> methods. But there is a persistent immutable list. Every time you make a new list, you re-use the contents of an existing immutable list; list3 re-uses the contents of list2, which it can do safely because list2 is never going to change.

    Would any of your answers also work on doubly-linked lists?

    You can of course easily make a doubly-linked list that does a full copy of the entire data structure every time, but that would be dumb; you might as well just use an array and copy the entire array.

    Making a persistent doubly-linked list is quite difficult but there are ways to do it. What I would do is approach the problem from the other direction. Rather than saying “can I make a persistent doubly-linked list?” ask yourself “what are the properties of a doubly-linked list that I find attractive?” List those properties and then see if you can come up with a persistent data structure that has those properties.

    For example, if the property you like is that doubly-linked lists can be cheaply extended from either end, cheaply broken in half into two lists, and two lists can be cheaply concatenated together, then the persistent structure you want is an immutable catenable deque, not a doubly-linked list. I give an example of a immutable non-catenable deque here:

    http://blogs.msdn.com/b/ericlippert/archive/2008/02/12/immutability-in-c-part-eleven-a-working-double-ended-queue.aspx

    Extending it to be a catenable deque is left as an exercise; the paper I link to on finger trees is a good one to read.

    UPDATE:

    according to the above we need to copy prefix up to the insertion point. By logic of immutability, if w delete anything from the prefix, we get a new list as well as in the suffix… Why to copy only prefix then, and not suffix?

    Well consider an example. What if we have the list (10, 20, 30, 40), and we want to insert 25 at position 2? So we want (10, 20, 25, 30, 40).

    What parts can we reuse? The tails we have in hand are (20, 30, 40), (30, 40) and (40). Clearly we can re-use (30, 40).

    Drawing a diagram might help. We have:

    10 ----> 20 ----> 30 -----> 40 -----> Empty
    

    and we want

    10 ----> 20 ----> 25 -----> 30 -----> 40 -----> Empty
    

    so let’s make

    | 10 ----> 20 --------------> 30 -----> 40 -----> Empty
    |                        /
    | 10 ----> 20 ----> 25 -/ 
    

    We can re-use (30, 40) because that part is in common to both lists.

    UPDATE:

    Would it be possible to provide the code for random insertion and deletion as well?

    Here’s a recursive solution:

    ImmutableList InsertAt(int value, int position)
    {
        if (position < 0) 
            throw new Exception();
        else if (position == 0) 
             return this.Add(value);
        else 
            return tail.InsertAt(value, position - 1).Add(head);
    }
    

    Do you see why this works?

    Now as an exercise, write a recursive DeleteAt.

    Now, as an exercise, write a non-recursive InsertAt and DeleteAt. Remember, you have an immutable linked list at your disposal, so you can use one in your iterative solution!

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

Sidebar

Related Questions

I'm asking this question despite having read similar but not exactly what I want
This is another question about well disposing objects from .NET. After having read a
After googling and landing on SO and having read this other question Is it
I am fairly certain this is a ten penny closure question. But having read
I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws
Having read this past question for git, I would like to ask if there
After having checked extensively for an answer to this question, I still cannot find
Having read this question on generating getters and setters in Visual Studio and tried
I have read this question and I am having the same problem: Put text
I apologize if this question is a bit broad, but I'm having a difficult

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.