简体   繁体   中英

Linked List in Java method implementation

Now Im preparing for coding interview and I have 1 question regarding linked list in Java. Can you tell me some reliable sources from which I can learn and practice the basic linked list methods. I liked this one: www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/code/LinkedList.java but I'm confused with some method implementations. For example the method E get(int pos) returns NOT node but the data E contained in the node at position pos. While here http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/LinkedList.java#LinkedList the method Node node(int index) returns the node at that position (not the data contained in it). Which implementation should I follow?

Well the Data Structures is a very conceptual and context based discipline. The various implementations that exist for each data structure are based on the requirements and scope of the data structure.
One can even argue that the LinkedList implementation of the Collection API is buggy as it doesn't work well if multiple threads work on it concurrently. Then a synchronizedList from the Collections class need to be made or at least an implementation which works well with multiple threads need to be used.

Follow the minimum viable convention that gets you going, as the interviewer won't just ask you a LinkedList implementation. What the interviewer wants to know is if your concepts and coding skills are up to a certain mark or not.

Think of what you can do with a Linked List . For that you'll have to think as to what type of LinkedList you are actually considering as there are many different kinds of LinkedList s, like SinglyLinkedList , DoublyLinkedList , SkipList , etc.

Considering a SinglyLinkedList , your LinkedList implementation should at least have the following methods: add() , remove() , contains() , clear() , size() .

Following is my implementation of a SinglyLinkedList :

import java.util.Iterator;
import java.util.StringJoiner;

public class LinkedList<T> implements Iterable<T>
{
  private Node head;
  private Node tail;
  private int size;

  private class Node
  {
    private T value;
    private Node next;

    public Node(T value)
    {
      this.value = value;
    }
  }

  public void add(T value)
  {
    Node node = new Node(value);

    if (head == null)
    {
      head = node;
    }
    else
    {
      tail.next = node;
    }

    tail = node;
    ++size;
  }

  public boolean remove(T value)
  {
    Node previous = null;
    Node current = head;

    while (head != null)
    {
      if (current.value.equals(value))
      {
        if (previous != null)
        {
          previous.next = current.next;

          if (current.next == null)
          {
            tail = previous;
          }
        }
        else
        {
          head = current.next;

          if (head == null)
          {
            tail = null;
          }
        }

        --size;
        return true;
      }

      previous = current;
      current = current.next;
    }

    return false;
  }

  public boolean contains(T value)
  {
    Node current = head;

    while (current != null)
    {
      if (current.value.equals(value))
      {
        return true;
      }

      current = current.next;
    }

    return false;
  }

  public void clear()
  {
    head = null;
    tail = null;
    size = 0;
  }

  public int size()
  {
    return size;
  }

  @Override
  public Iterator<T> iterator()
  {
    return new Iterator<T>()
    {
      private Node current = head;

      @Override
      public boolean hasNext()
      {
        return current != null;
      }

      @Override
      public T next()
      {
        Node next = current;
        current = current.next;
        return next.value;
      }
    };
  }

  @Override
  public String toString()
  {
    StringJoiner joiner = new StringJoiner(", ");

    for (T value : this)
    {
      joiner.add(value.toString());
    }

    return joiner.toString();
  }
}

As you can see, my implementation may be different from the implementations that you may find elsewhere. Small differences are not any issue as long as the data structure's concepts aren't radically changed and as long as it works properly with its defined interface.

For disciplines like Data Structures, you have to think for yourself and based on your requirements, use or implement data structures that fit your needs. As far as interviews go, an implementation of a minimum viable data structure is all that you need to show and all that is required. Just make sure that such minimum viable data structure isn't buggy in its context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM