打印值小于参数中设置的值的节点

时间:2017-06-22 00:55:38

标签: java algorithm linked-list

我有这个代码,它应该在一个链表中,并返回比参数中指定的节点值小的节点,但它正好相反。我做错了什么?

static LinkedListNode removeNodes(LinkedListNode list, int x) {
    LinkedListNode current = list;
    while(current.next != null ){
        if (current.val >x){
            if (current.next.next == null){
                break;
            }
            else{
            current = current.next.next;
            }
        }
        else{
            current = current.next;
        }
    }
    return current;
}

1 个答案:

答案 0 :(得分:0)

要从链接列表中删除节点,您需要更改.next引用而不是当前值。

另外你的> x应该是< X

OpenTrace/ProcessTrace/StopTrace
相关问题