有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数据结构在java中不使用任何API从LinkedList中删除特定项

我正在尝试从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-->;尾巴
  • 拆卸后2-->;4-->;5-->;6-->;7-->;尾巴

在这里,当我设置current = current.next或引用被设置为下一个值时,我丢失了值1。所以我肯定对存储在不同引用中的数据的表示有一些问题


共 (2) 个答案

  1. # 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;
    }
    

    此外,澄清一下,这不是一个递归的解决方案

  2. # 2 楼答案

    错误就在这里:

    return previous;
    

    如果列表未删除,则应返回列表的原始标题。要以图形方式显示它,请执行以下操作:

    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                      ^
    

    此时,通过返回previous,您将丢失前一个head元素L

    对于要移除头部元素的情况,应该在循环之前添加一个单独的检查

    顺便说一句,你的Remove方法不是递归的——它从不调用自己