如何删除java中链表中的所有evens?其他类似的问题对我没有帮助.我有一个可能的解决方案,但它似乎太复杂了.我不确定它是否有效.
public class Node {
public int value;
public Node next;
public Node (int val) {
value = val;
}
public Node removeNode (Node root) {
if (root == null || (root.next == null && isOdd(root.value))) {
return null;
}
Node deep = root;
while (deep.next != null) {
deep = deep.next;
}
if (isOdd(deep.value)) {
for (Node x = root; x != null; x = x.next) {
if (isOdd(x.next.value)) {
x.next = x.next.next;
}
}
} else {
for …Run Code Online (Sandbox Code Playgroud)