Java LinkedList clone() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, clone() creates a new LinkedList that contains the same elements as the original, in the same order. Adding or removing elements in one list does not change the other.

The copy is shallow: the elements are not cloned, so both lists point to the same objects. For immutable values such as String and Integer this makes no difference. For mutable objects, a change made through one list is visible through the other.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It overrides Object.clone(), and LinkedList implements Cloneable.

Syntax

clone() returns Object, so a cast is needed.

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B"));
@SuppressWarnings("unchecked")
LinkedList<String> copy = (LinkedList<String>) list.clone();
LinkedList<String> copy2 = new LinkedList<>(list);   // no cast needed
CallWhat it does
(LinkedList<E>) list.clone()A shallow copy; needs a cast and gives an unchecked warning.
new LinkedList<>(list)The usual way to copy: no cast, no warning.
copy.addAll(list)Copies into a list that already exists.
list.toArray()Copies the elements into an array instead.

Most programmers prefer the copy constructor new LinkedList<>(list). It is shorter, needs no cast, and gives the same shallow copy.

Library declarations
Java
// java.util.LinkedList
public Object clone()

Parameters

ParameterDescription
Noneclone() takes no parameters.

Return Value

Return ValueDescription
ObjectA new LinkedList with the same elements. Cast it to LinkedList<E>.

Compare with Similar Methods

Ways to copy a list:

MethodHow it differs
addAll(c)Copies elements into an existing list.
toArray()Copies the elements into an array.
subList(a, b)A view of part of the list, not a copy.
ArrayList.clone()The same method on an array-backed list.

Java Examples

Example 1 — Clone a List and Change One of Them

Clone a list, add to the original and remove from the copy, and print both.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListCloneDemo {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        LinkedList<String> original = new LinkedList<>(Arrays.asList("red", "green", "blue"));
        LinkedList<String> copy = (LinkedList<String>) original.clone();

        original.add("pink");
        copy.removeFirst();

        System.out.println("original: " + original);
        System.out.println("copy:     " + copy);
        System.out.println("same object: " + (original == copy));
    }
}

How It Works

  1. The cast. clone() returns Object, so it is cast to LinkedList<String>. The annotation silences the unchecked warning.
  2. Two lists. The copy starts with red, green and blue, just like the original.
  3. Changes stay separate. Adding "pink" to the original does not add it to the copy, and removing "red" from the copy does not affect the original.
  4. Different objects. original == copy is false, because clone() creates a new list.

Example 2 — See That Mutable Elements Are Shared

Clone a list of StringBuilder objects, change one element, and look at both lists.

Java
import java.util.LinkedList;

public class LinkedListCloneShallow {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        LinkedList<StringBuilder> names = new LinkedList<>();
        names.add(new StringBuilder("Ann"));
        names.add(new StringBuilder("Bob"));

        LinkedList<StringBuilder> copy = (LinkedList<StringBuilder>) names.clone();

        copy.get(0).append("-changed");
        copy.add(new StringBuilder("Cy"));

        System.out.println("names: " + names);
        System.out.println("copy:  " + copy);
        System.out.println("same first element: " + (names.get(0) == copy.get(0)));
    }
}

How It Works

  1. Mutable elements. A StringBuilder can be changed in place, unlike a String.
  2. The copy shares the objects. copy.get(0) is the very same StringBuilder as names.get(0).
  3. A change shows in both lists. Appending to the shared object makes "Ann-changed" appear in both outputs.
  4. The structure is separate. Adding "Cy" to the copy does not add it to the original, and the last line confirms that the first elements are identical objects.

Example 3 — Copy a List Without clone

Copy a list with the copy constructor and with addAll, and compare the results.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListCloneCtor {
    public static void main(String[] args) {
        LinkedList<Integer> source = new LinkedList<>(Arrays.asList(7, 8, 9));

        LinkedList<Integer> viaCtor = new LinkedList<>(source);

        LinkedList<Integer> viaAddAll = new LinkedList<>();
        viaAddAll.add(1);
        viaAddAll.addAll(source);

        source.clear();

        System.out.println("source:    " + source);
        System.out.println("viaCtor:   " + viaCtor);
        System.out.println("viaAddAll: " + viaAddAll);
    }
}

How It Works

  1. No cast needed. new LinkedList<>(source) already has the right type, so it needs no warning suppression.
  2. addAll copies into an existing list. viaAddAll starts with 1, then receives 7, 8 and 9 after it.
  3. The source is cleared. Both copies were made before this, so they do not depend on the source any more.
  4. Result. The source is empty, viaCtor is 7, 8, 9, and viaAddAll is 1, 7, 8, 9.

Frequently Asked Questions

Use new LinkedList<>(list), which is the simplest way. You can also use (LinkedList<E>) list.clone().
No. It is a shallow copy: the new list holds references to the same element objects.
Its return type is Object, because it is declared that way in Object and Cloneable. Cast it to LinkedList<E>.
The compiler cannot verify the generic type in the cast. Add @SuppressWarnings("unchecked"), or use the copy constructor instead.
Adding or removing elements does not. But changing a mutable element object that both lists share is visible in both.

Next: contains()

Check whether the list holds a given element.

contains() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful