0

我正在尝试从 java 中的 LinkedList 中删除一个项目。这个列表是由我实现的,我没有使用任何 java API。我面临的主要问题是递归,因为我总是迷失在递归编码中。

class List{

    int N;
    List next;
    List current;
    List(int N){
        this.N =N;
        this.next = null;
    }

    @Override
    public String toString() {
        String o = "";
        List curr = this;
        while(curr != null){
            o += curr.N+"-->";
            curr = curr.next;
        }

        return o+"TAIL";
    }
}

实现的方法:

private static List Remove(List L,int N){
    if(L == null || L.next == null)
        return L;

    List current = L;
    List previous = null;

    while(current != null){
        if(current.N == N){
            current = current.next;
            if(previous == null)previous = current;
            else{
                previous.next = current;
            }
            break;
        }else{
            previous = current;
            current = current.next;             
        }   
    }
    return previous;
}

输入 -

List list1 = new List(1);
        list1.next = new List(2);
        list1.next.next = new List(3);
        list1.next.next.next  = new List(4);
        list1.next.next.next.next  = new List(5);
        list1.next.next.next.next.next  = new List(6);
        list1.next.next.next.next.next.next  = new List(7);
System.out.println("Before Removal "+list1.toString());
        System.out.println("After Removal "+Remove(list1,3));

我得到的输出是 -

  • 去除前 1-->2-->3-->4-->5-->6-->7-->TAIL
  • 去除后2-->4-->5-->6-->7-->TAIL

在这里,当我设置current = current.nextor 参考被设置为下一个值时,我失去了值 1。所以肯定我在展示存储在不同参考中的数据时遇到了一些问题。

4

2 回答 2

2

The mistake is here:

return previous;

You should return the original head of the list if it was not removed. To show it graphically:

N == 3
List Before Removal: 1-->2-->3-->4-->5-->6-->7-->TAIL
At start of iteration 1:
L                    ^
previous      (null)
current              ^
No match -> iteration 2:
L                    ^
previous             ^
current                  ^
No match -> iteration 3:
L                    ^
previous                 ^
current                      ^
Match -> remove current:
List After Removal:  1-->2-->4-->5-->6-->7-->TAIL
L                    ^
previous                 ^
current                      ^

At this point by returning previous, you lose the former head element L.

For the case when the head element is to be removed, you should add a separate check before the loop.

Btw your Remove method is not recursive - it is never calling itself.

于 2012-04-25T09:17:51.597 回答
1

这只是因为您没有返回头部 - 而是指向您刚刚“删除”的节点的前一个指针:

static List Remove(final List L, final int N) {
    // Base case for null head pointer  
    final List head = L;
    if (head == null)
        return head;

    // Base case for removing the head
    if (head.N == N)
       return head.next;

    List current = head.next;
    List previous = head;

    while (current != null) {
        if (current.N == N) {
            current = current.next;
            if (previous == null) {
                previous = current;
            }
            else {
                previous.next = current;
            }

            break;
        } else {
            previous = current;
            current = current.next;
        }
    }

    return head;
}

另外-澄清一下-这不是递归解决方案。

于 2012-04-25T09:23:44.650 回答