Sha256: 7daf6615a783d4988e53af9be17855ddf9d3ecc80814a0fe289dfa98aebcb024

Contents?: true

Size: 1.88 KB

Versions: 179

Compression:

Stored size: 1.88 KB

Contents

import java.lang.reflect.Array;
import java.util.NoSuchElementException;
import java.util.Objects;

public class SimpleLinkedList<T> {

    private static class Element<T> {

        final T value;
        Element next;

        Element(T value) {
            this.value = value;
        }
    }

    private Element<T> head;
    private int size;

    public SimpleLinkedList() {
    }

    public SimpleLinkedList(T[] values) {
        for (int ii = values.length - 1; ii >= 0; ii--) {
            push(values[ii]);
        }
    }

    public final void push(T value) {
        Element<T> newElement = new Element<>(value);
        this.size++;
        if (Objects.isNull(head)) {
            head = newElement;
        } else {
            newElement.next = head;
            head = newElement;
        }
    }

    public T pop() {
        if (Objects.isNull(head)) {
            throw new NoSuchElementException();
        }
        T value = head.value;
        head = head.next;
        this.size--;
        return value;
    }

    public void reverse() {
        Element<T> current = head;
        Element<T> next;
        Element<T> previous = null;
        while (Objects.nonNull(current)) {
            next = current.next;
            current.next = previous;
            previous = current;
            current = next;
        }
        head = previous;
    }

    public T[] asArray(Class<T> clazz) {
        T[] result = newArray(clazz, this.size);
        int index = 0;
        Element<T> current = head;
        while (Objects.nonNull(current)) {
            result[index++] = current.value;
            current = current.next;
        }
        return result;
    }

    private <T> T[] newArray(Class<T> clazz, int size) {
        @SuppressWarnings("unchecked")
        T[] arr = (T[]) Array.newInstance(clazz, size);

        return arr;
    }

    public int size() {
        return this.size;
    }
}

Version data entries

179 entries across 179 versions & 1 rubygems

Version Path
trackler-2.1.0.23 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.22 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.21 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.20 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.19 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.18 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.17 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.16 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.15 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.14 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.13 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.12 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.11 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.10 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.9 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.8 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.7 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.6 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.5 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java
trackler-2.1.0.4 tracks/java/exercises/simple-linked-list/src/example/java/SimpleLinkedList.java