右旋转链接列表递归
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了右旋转链接列表递归相关的知识,希望对你有一定的参考价值。
我需要创建一个方法来旋转或移动LinkedList
中的元素,这个元素称为ListItem
,我的程序在递归时向右移动。例如,我有1->2->3->4
方法之后的List 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
}
答案
遍历ListItem直到最后一项,这将成为要返回的ListItem。接下来添加输入lst
to。
ListItem<T> returnList = this.ringShiftRight(lst.next);
lst.next.next = lst;
lst.next = null;
return returnList;
以上是关于右旋转链接列表递归的主要内容,如果未能解决你的问题,请参考以下文章