public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node…
- User
public class SinglyLinkedList implements Cloneable { //---------------- nested Node class ---------------- /**
- Node of a singly linked list, which stores a reference to its
- element and to the subsequent node in the list (or null if this
- is the last node). */ private static class Node {
/** The element stored at this node */private E element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node<E> next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n reference to a node that should follow the new node */ public Node(E e, Node<E> n) { element = e; next = n; } // Accessor methods /** * Returns the element stored at the node. * @return the element stored at the node */ public E getElement() { return element; } /** * Returns the node that follows this one (or null if no such node). * @return the following node */ public Node<E> getNext() { return next; } // Modifier methods /** * Sets the node's next reference to point to Node n. * @param n the node that should follow this one */ public void setNext(Node<E> n) { next = n; }} //----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList /** The head node of the list */ private Node head = null; // head node of the list (or null if empty)
/** The last node of the list */ private Node tail = null; // last node of the list (or null if empty)
/** Number of nodes in the list */ private int size = 0; // number of nodes in the list
/** Constructs an initially empty list. */ public SinglyLinkedList() { } // constructs an initially empty list
// access methods /**
- Returns the number of elements in the linked list.
- @return number of elements in the linked list */ public int size() { return size; }
/**
- Tests whether the linked list is empty.
- @return true if the linked list is empty, false otherwise */ public boolean isEmpty() { return size == 0; }
/**
- Returns (but does not remove) the first element of the list
- @return element at the front of the list (or null if empty) */ public E first() { // returns (but does not remove) the first element if (isEmpty()) return null; return head.getElement(); }
/**
- Returns (but does not remove) the last element of the list.
- @return element at the end of the list (or null if empty) */ public E last() { // returns (but does not remove) the last element if (isEmpty()) return null; return tail.getElement(); }
- Assistant
Here is one possible implementation for the method to concatenate two singly linked lists:
public static <E> SinglyLinkedList<E> concatenate(SinglyLinkedList<E> list1, SinglyLinkedList<E> list2) { SinglyLinkedList<E> concatenatedList = new SinglyLinkedList<>(); Node<E> current = list1.head; while (current != null) { concatenatedList.addLast(current.getElement()); current = current.getNext(); } current = list2.head; while (current != null) { concatenatedList.addLast(current.getElement()); current = current.getNext(); } return concatenatedList; }The method creates a new empty list, then iterates over the nodes of and adds each element to the end of the new list using . It then does the same for . Finally, it returns the new concatenated list.