I have this code public class SinglyLinkedList<E> implements Cloneable {…
- User
I have this code 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
public void swapTwoNodes(Node node1, Node node2) { if (node1 == null || node2 == null) return; if (node1 == node2) return;
// Determine which node comes first in the list Node current = head; Node prev1 = null, prev2 = null; while (current != null && current != node1 && current != node2) { prev1 = (current == node1) ? null : prev1; prev2 = (current == node2) ? null : prev2; current = current.getNext(); }
// Either node1 or node2 (or both) is not in the list if (current == null) return;
// Swap node1 and node2 if (prev1 != null) prev1.setNext(node2); else head = node2; if (prev2 != null) prev2.setNext(node1); else head = node1; Node temp = node1.getNext(); node1.setNext(node2.getNext()); node2.setNext(temp); }
// Test method public static void main(String[] args) { SinglyLinkedList list = new SinglyLinkedList<>(); list.addFirst(3); list.addFirst(2); list.addFirst(1); list.addLast(4); list.addLast(5); System.out.println("Before swapping: " + list); // Output: (1, 2, 3, 4, 5) SinglyLinkedList.Node node1 = list.head.getNext(); SinglyLinkedList.Node node2 = node1.getNext().getNext(); list.swapTwoNodes(node1, node2); System.out.println("After swapping: " + list); // Output: (1, 4, 3, 2, 5) }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.