右旋转链接列表递归

时间:2018-03-05 16:43:34

标签: java recursion linked-list rotation

我需要创建一个方法来循环或移动LinkedList中的元素,这个元素被称为ListItem,我的程序在递归时向右移动。例如,我在1->2->3->4方法后面有列表4->1->2->3。 Method应该返回旋转的List。

因此,这可能对该方法有所帮助:

public class ListItem<T>
{

    public T key;
    public ListItem<T> next;

    /**
     * Constructor of this Class
     * 
     * @param key
     *            the key of the ListItemAbstract
     */
    public ListItem(T key)
    {
        this.key = key;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean equals(Object obj)
    {
        if (obj == null || obj.getClass() != this.getClass())
            return false;
        if (this.key == null && ((ListItem<T>) obj).key == null)
            return true;
        if (this.key != null && this.key.equals(((ListItem<T>) obj).key))
        {
            if (this.next == null)
                if (((ListItem<T>) obj).next == null)
                    return true;
                else
                    return false;
            return this.next.equals(((ListItem<T>) obj).next);
        }
        else
            return false;
    }

    /**
     * This methode returns the Element on the given position.
     * 
     * @param pos
     *            the position to return the key from. Position 1 means the current element
     * @return the key of the element at pos
     * @throws IllegalArgumentException
     *             if the position ist not in the list
     */
    public T get(int pos) throws IllegalArgumentException
    {
        // recursion stop
        if (pos == 1)
        {
            return this.key;
        }
        else if (pos > 1)
        {
            // recursion start: call this method again for the next element of the list,
            // but check for NullpointerException first
            if (next != null)
            {
                return next.get(pos - 1);
            }
            else
            {
                //
                throw new IllegalArgumentException("the index is greater than the number of elements in this list");
            }
        }
        else
        {
            throw new IllegalArgumentException("the position is less than 1, but must be equal to or higher than 1");
        }
    }

    /**
     * This method returns the size of the List
     * 
     * @return the size of this list
     */
    public int getSize()
    {
        if (this.key == null && this.next == null)
        {
            // recursion stop: no next element and this key is null -> don't add it to the size
            return 0;
        }
        else if (this.next == null) // deleting this if-case causes a NullpointerException
        {
            // recursion stop: no next element, but this key is not null -> add it to the size
            return 1;
        }
        else
        {
            // recursion start: recall this method for the next element and increase the size by one
            return this.next.getSize() + 1;
        }
    }

    /**
     * Inserts an key into this list.
     * 
     * @param key
     *            the key to insert.
     * @throws IllegalArgumentException
     *             if key is null
     */
    public void insert(T key) throws IllegalArgumentException
    {
        if (key == null)
        {
            throw new IllegalArgumentException("Cannot insert null");
        }
        else if (this.key == null && this.next == null)
        {
            this.key = key;
        }
        else
        {
            ListItem<T> p = this;
            while (p.next != null)
            {
                p = p.next;
            }
            p.next = new ListItem<T>(key);
        }
    }
}

这是我到目前为止的方法:

/**
     * The method shifts the elements of the List to the right.
     * 
     * @param lst
     *            the list to work on
     * @return the new list
     */
    public ListItem<T> ringShiftRight(ListItem<T> lst) {
        if (lst == null) {
            return null;
        }
        if (lst.next == null) {
            return lst;
        } 
        // TODO Here I have no Idea how to create the method any further 
    }

1 个答案:

答案 0 :(得分:-2)

遍历ListItem直到最后一项,这将成为要返回的ListItem。将输入lst添加到下一个。

    ListItem<T> returnList = this.ringShiftRight(lst.next);
    lst.next.next = lst;
    lst.next = null;
    return returnList;