如何从链表java中删除特定值?

时间:2013-06-26 22:39:00

标签: java

public class LinkList
{
private Node list;
private int count;  // number of values stored in the list

public LinkList(){
    list = null;
    count = 0;
    }

// Returns number of values in the LinkList
public int count() {
    return count;
}



/*
 * delete all nodes with this value
 * if the count is zero do nothing
 * if count is 1 delete front node (can use front() and ignore return)
 * else travel the list and when found make prev.next cur.next ... return
 * if still in method delete rear node.
 *
 */


public void deleteNode(int value)
{

    }


}


public class Node{

int value;
Node next;

public Node() {
    next=null;
}

public Node(int n, Node nextNode){
    setValue(n);
    setNext(nextNode);
}

public int getValue(){
    return value;
}

public void setValue(int n){
    value = n;
}

public Node getNext(){
    return next;
}

public void setNext(Node node){
    next = node;
}

public boolean equals(Node other) {
    return (this.getValue() == other.getValue());
}

public String toString() {
    return "" + value;
}

}

3 个答案:

答案 0 :(得分:1)

                    +------+        +------+        +------+
                    |      |        |      |        |      |
            Step 0  |      |+------>|      |+------>|      |
                    |      |        |      |        |      |
                    |      |        |      |        |      |
                    +------+        +------+        +------+

                               +----------------+
                    +------+   |    +------+    |   +------+
                    |      |+--+    |      |    +-->|      |
            Step 1  |      |        |      |+------>|      |
                    |      |        |      |        |      |
                    |      |        |      |        |      |
                    +------+        +------+        +------+


                    +------+                        +------+
                    |      |                        |      |
            Step 2  |      |+---------------------->|      |
                    |      |                        |      |
                    |      |                        |      |
                    +------+                        +------+

答案 1 :(得分:0)

首先,使用Java现有的ArrayList实现。大多数操作都处于O(n)线性时间,有些甚至是O(1)。它还可以很好地与集合,迭代器和集合集成。

我建议使用双向链表。只需添加一个previous字段,其中包含必需的getter和setter。

然后删除,只需将相关节点周围的前一个和下一个链接在一起。这就是留给读者的练习。 (或在修订历史中找到它) 这只是将链接放回到当前所选节点周围。

如果要使用单链表,则必须遍历列表,并检查next是否是要删除的节点。然后将当前节点的下一个设置为nextNode.getNext()。

后一种方法效率很低。

答案 2 :(得分:0)

您希望在值不为空时遍历链接列表。如果您遇到了您尝试删除的值,请删除该节点,重新处理链接并返回。你的功能应如下所示:

public void deleteNode(int value)
{
    if(count == 0) {
        return;
    } else if (count == 1){
        // delete this node. I'm not quite sure what you mean by use front() in your instruction
    } else {
        Node x = Node first; // This should be the first node in the list
        while(x != null && x.next != null) {
            if(x.value == value) {
                if(x == first) {
                    first = x.next; // If your first node is bad, make 2nd node your first node
                } else {
                    prev.next = x.next; // This skips the current node, with the sought-after value
                }
            } else {
                x = x.next; // Traverse to check the next node
            }
        }
    }
}

请记住,您需要在节点类中声明prev和first节点。不要以为我看到了给定代码中定义的那些。