代码删除错误的节点

时间:2014-02-13 20:32:30

标签: java linked-list nodes

我正在学习操作节点,并且遇到了我的一个测试用例的问题,似乎我的代码没有删除正确的节点。代码应该是删除包含小写字符的节点,但是当测试用例期望E时,我会返回l。有人可以查看我的代码和测试用例并解释错误。我不能使用静态变量,数组或Java集合。

我的代码

public class ListUppercaseExample {

    public static Node<Character> uppercase(Node<Character> head) {
        if (head == null) {
            return null;
        }
        Node<Character> prev = head;
        Node<Character> start = head;

        while (head != null) {
            if (Character.isLowerCase(head.value)) {
                if (Character.isLowerCase(start.value)) {
                    start = head.next;
                    prev = head.next;
                    head = head.next;
                } else {
                    if(head.next == null){
                        return start;
                    }
                    else{
                        prev.next = head.next;
                        head = head.next;
                    }
                }
            } else {
                head = head.next;
            }
        }

        return start;
    }
}

我的节点类

public final class Node<T> {
    public final T value;
    public Node<T> next;

    public Node(T _value) {
        this( _value, null );
    }

    public Node(T _value, Node<T> _next) {
        value = _value;
        next  = _next;
    }

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

测试用例

@Test
public void testDrEvil() {
    @SuppressWarnings("unchecked")
    Node<Character>[] nodes = new Node[] { 
        new Node<Character>( 'd' ), // 0
        new Node<Character>( 'R' ), // 1
        new Node<Character>( 'E' ), // 2
        new Node<Character>( 'v' ), // 3
        new Node<Character>( 'I' ), // 4
        new Node<Character>( 'l', null )
    };

    for (int i = 0; i < nodes.length-1; i++) {
        nodes[ i ].next = nodes[ i+1 ];
    }

    // expected: D -> R
    Node<Character> actual = ListUppercaseExample.uppercase(nodes[0]);
    Node<Character> now = actual;
    assertEquals("", nodes[1], now); // R
    assertEquals("", nodes[2], now.next); // E
    now = now.next;
    assertEquals("", nodes[4], now.next); // I
    now = now.next;
    assertEquals("", null, now.next);
}

1 个答案:

答案 0 :(得分:2)

你有两个问题:

  1. 如果由于while循环中的这一行而导致最后一个值为小写,则不会删除列表中的最后一个值:

    if(head.next == null){
        return start;
    }
    

    如果它是小写的,您希望跳过head的当前值,无论下一个值是否为null。所以你可以删除if语句的那一部分,这样你的代码就像这样:

    if (Character.isLowerCase(start.value)) {
        start = head.next;
        prev = head.next;
        head = head.next;
    } else {
        prev.next = head.next;
        head = head.next;
    }
    
  2. 您永远不会更新prev。您需要将prev设置为head 字符是大写之前更改的值 head。如果字符是大写的,你的else语句应该看起来 像这样:

    } else {
        prev = head;
        head = head.next;
    }
    
  3. 完成这些更改后,您的while循环应如下所示:

      while (head != null) {
            if (Character.isLowerCase(head.value)) {
                if (Character.isLowerCase(start.value)) {
                    start = head.next;
                    prev = head.next;
                    head = head.next;
                } else {
                    prev.next = head.next;
                    head = head.next;
                }
            } else {
                prev = head;
                head = head.next;
            }
        }