简体   繁体   English

在不使用任何API的情况下从Java中的LinkedList删除特定项目

[英]Removing a specific item from LinkedList in java without using any API

I am trying to remove an item from the LinkedList in java. 我正在尝试从Java中的LinkedList中删除一个项目。 This List is implemented by me and I am not using any java API. 该列表由我实现,我没有使用任何Java API。 The major trouble I am facing is with RECURSION as I am always lost in recursion coding. 我面临的主要麻烦是RECURSION,因为我总是在递归编码中迷路。

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";
    }
}

Method implemented: 实现的方法:

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;
}

Input - 输入-

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));

Output I am getting is - 我得到的输出是-

  • Before Removal 1-->2-->3-->4-->5-->6-->7-->TAIL 移除前1-> 2-> 3-> 4-> 5-> 6-> 7-> TAIL
  • After Removal 2-->4-->5-->6-->7-->TAIL 移除后2-> 4-> 5-> 6-> 7-> TAIL

Here I am losing the value 1 as I am setting the current = current.next or reference is being set to next value. 在这里,当我设置current = current.next或引用被设置为下一个值时,我将丢失值1。 So definitely I am having some problem with the presentation of data stored in different references. 因此,毫无疑问,我在存储存储在不同引用中的数据时遇到了一些问题。

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 . 此时,通过返回previous ,您将失去前一个头部元素L

For the case when the head element is to be removed, you should add a separate check before the loop. 对于要删除head元素的情况,应在循环之前添加单独的检查。

Btw your Remove method is not recursive - it is never calling itself. 顺便说一句,您的Remove方法不是递归的-它永远不会自我调用。

It's simply because you are not returning the head - but instead the previous pointer to the node you just 'removed': 这仅仅是因为您没有返回头-而是指向您刚刚“删除”的节点的前一个指针:

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;
}

Also - to clarify - this is not a recursive solution. 另外-要澄清-这不是递归解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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