Reputation: 110
I have wrote this code, which will do a search for chars in a linkedlist(called ListNode), checking if there are any chars which is in uppercase, and then storing them in a new linkedlist(and then return the new linkedlist). When I do run this code, it goes in a "infinite" loop not returning anything. Why is that? The NodeList does contain some uppercase chars. Here is the code:
public static ListNode copyUpperCase(ListNode head) {
ListNode newListNode = mkEmpty();
if(head == null){
throw new ListsException("");
}else{
while(head.next != null){
if(Character.isUpperCase(head.element)){
newListNode.element = head.element;
head = head.next;
}
}
}
return newListNode;
}
Upvotes: 1
Views: 73
Reputation: 178333
What if the head.element
character isn't uppercase? The head
is never updated to head.next
.
Place head = head.next;
after your if
block, so it's always executed.
Upvotes: 5