Java 链接列表,检查双值
我正在编写一个代码,如果整个链接列表都有结巴,则返回true;如果没有结巴,则返回false。口吃列表应该是Java 链接列表,检查双值,java,list,Java,List,我正在编写一个代码,如果整个链接列表都有结巴,则返回true;如果没有结巴,则返回false。口吃列表应该是1,1,2,2,5,5,8,8,非口吃列表应该是1,1,2,2,5,6,8,8 我已经玩了很长一段时间了,似乎无法让它返回正确的语句,也无法获得nullpointer异常 public boolean foo(){ ListNode current = front; ListNode runner = current.next; while (current
1,1,2,2,5,5,8,8
,非口吃列表应该是1,1,2,2,5,6,8,8
我已经玩了很长一段时间了,似乎无法让它返回正确的语句,也无法获得nullpointer异常
public boolean foo(){
ListNode current = front;
ListNode runner = current.next;
while (current.next.next!=null){ //Looks two ahead for the end
if(current.data!=runner.data){ //They aren't equal, false
System.out.println(current.data); //just to see my data
System.out.println(runner.data); //debugging only
return false;
}
current = current.next.next; //increase by 2
runner = runner.next.next; // increase by 2
System.out.println(current.data + " ||" + runner.data); //again debugging
}
return true; // didn't register false, go ahead and true dat badboy.
}
public static void main (String[] args){
LinkedIntList list = new LinkedIntList();
list.add(1);
list.add(1);
list.add(3);
list.add(3);
list.add(5);
list.add(5);
System.out.println(list.foo());
}
有人看到这里有明显的错误吗?我已经试着运行我的while循环来获得current.next,以及每次增加我的runner和current一个,而不是两个,但都没有效果 而不是while(current.next.next!=null)
,请选中while(runner.next!=null)
。此外,还必须在while
循环之后匹配最后两个节点的数据
假设列表中有偶数个元素,如您在问题中提到的,那么代码中的以下更改将产生正确的输出
public boolean foo(){
ListNode current = front;
ListNode runner = current.next;
while (runner.next!=null){ //Looks two ahead for the end
if(current.data!=runner.data){ //They aren't equal, false
System.out.println(current.data); //just to see my data
System.out.println(runner.data); //debugging only
return false;
}
current = current.next.next; //increase by 2
runner = runner.next.next; // increase by 2
System.out.println(current.data + " ||" + runner.data); //again debugging
}
if(current.data!=runner.data){ //They aren't equal, false
System.out.println(current.data); //just to see my data
System.out.println(runner.data); //debugging only
return false;
}
return true; // didn't register false, go ahead and true dat badboy.
}
更好且干净的实现如下所示:
public boolean foo(){
ListNode current = front;
while (current != null){
if(current.next == null)
return false;
if(current.data != current.next.data)
return false;
current = current.next.next;
}
return true;
}
不要选择while(current.next.next!=null)
,而是选择while(runner.next!=null)
。此外,还必须在while
循环之后匹配最后两个节点的数据
假设列表中有偶数个元素,如您在问题中提到的,那么代码中的以下更改将产生正确的输出
public boolean foo(){
ListNode current = front;
ListNode runner = current.next;
while (runner.next!=null){ //Looks two ahead for the end
if(current.data!=runner.data){ //They aren't equal, false
System.out.println(current.data); //just to see my data
System.out.println(runner.data); //debugging only
return false;
}
current = current.next.next; //increase by 2
runner = runner.next.next; // increase by 2
System.out.println(current.data + " ||" + runner.data); //again debugging
}
if(current.data!=runner.data){ //They aren't equal, false
System.out.println(current.data); //just to see my data
System.out.println(runner.data); //debugging only
return false;
}
return true; // didn't register false, go ahead and true dat badboy.
}
更好且干净的实现如下所示:
public boolean foo(){
ListNode current = front;
while (current != null){
if(current.next == null)
return false;
if(current.data != current.next.data)
return false;
current = current.next.next;
}
return true;
}
您不能在没有先进行一些检查的情况下盲目使用
current.next.next
,因为current
或current.next
都可能为空
如果是这种情况,您将遇到空指针问题
假设stuttered仅表示double(根据您的示例),而不是任何倍数,则最好使用以下算法:
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not a pair.
if node.data != node.next.data:
return false
# Advance to next pair, okay as we have 2+ items left.
node = node.next.next
return true
stuttered = isStuttered(head)
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not at least two.
val = node.data
if val != node.next.data:
return false
# Start with second item in set,
# advance to either new value or list end.
node = node.next
while node != null # note 'and' must short-circuit
and node.data == val:
node = node.next
return true
另一方面,如果“口吃”意味着每个项目中有两个或两个以上,那么这是算法的一个小变化:
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not a pair.
if node.data != node.next.data:
return false
# Advance to next pair, okay as we have 2+ items left.
node = node.next.next
return true
stuttered = isStuttered(head)
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not at least two.
val = node.data
if val != node.next.data:
return false
# Start with second item in set,
# advance to either new value or list end.
node = node.next
while node != null # note 'and' must short-circuit
and node.data == val:
node = node.next
return true
您不能在没有先进行一些检查的情况下盲目使用
current.next.next
,因为current
或current.next
都可能为空
如果是这种情况,您将遇到空指针问题
假设stuttered仅表示double(根据您的示例),而不是任何倍数,则最好使用以下算法:
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not a pair.
if node.data != node.next.data:
return false
# Advance to next pair, okay as we have 2+ items left.
node = node.next.next
return true
stuttered = isStuttered(head)
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not at least two.
val = node.data
if val != node.next.data:
return false
# Start with second item in set,
# advance to either new value or list end.
node = node.next
while node != null # note 'and' must short-circuit
and node.data == val:
node = node.next
return true
另一方面,如果“口吃”意味着每个项目中有两个或两个以上,那么这是算法的一个小变化:
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not a pair.
if node.data != node.next.data:
return false
# Advance to next pair, okay as we have 2+ items left.
node = node.next.next
return true
stuttered = isStuttered(head)
def isStuttered(node):
while node != null:
# Check if only one item left.
if node.next == null:
return false
# Check if not at least two.
val = node.data
if val != node.next.data:
return false
# Start with second item in set,
# advance to either new value or list end.
node = node.next
while node != null # note 'and' must short-circuit
and node.data == val:
node = node.next
return true
什么是
perfectStutter
方法?你把上面的方法叫做foo,它应该是perfectStutter吗?什么是perfectStutter
方法?您调用了上面的方法foo,它应该是perfectStutter吗?当它为false时,代码仍然返回true。如果我将其中一个5改为6,我会得到这个返回值3 | | 3 5 | | 6 true。还没有检查你的代码块,我会让你知道这是否有效!当代码为false时仍然返回true。如果我将其中一个5改为6,我会得到这个返回值3 | | 3 5 | | 6 true。还没有检查你的代码块,我会让你知道这是否有效!