我正在寫一個代碼,如果整個鏈接列表結束,則返回true,否則返回false。一個結巴的名單將是1,1,2,2,5,5,8,8
,非結巴將是像1,1,2,2,5,6,8,8
。LinkList,檢查雙值
我一直在玩弄它相當長一段時間,似乎並不能得到它返回正確的語句或無法獲得空指針異常。
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,以及增加我的跑步者和當前每次一個而不是兩個,但沒有任何工作。
什麼是'perfectStutter'方法?你把它叫做foo的方法,它應該是perfectStutter嗎? – baseballlover723