I have an array of characters and I'm trying to convert each character into a node that links to the next node in line. The problem is I keep getting caught in infinite loops and I have no idea why. Here's my code:
String map = "ABBACBCCA";
char[] charArray = map.toCharArray();
ListNode head;
ListNode temp;
ListNode next;
for (int i = 0; i < charArray.length - 1; i++) {
temp = new ListNode(charArray[i]);
next = new ListNode(charArray[i+1]);
temp.next = next;
if (i == 0) {
head = temp;
}
}
And the ListNode class looks like:
class ListNode<T> {
public T data = null;
public ListNode next = null;
public ListNode(T data) {
this.data = data;
}
}
It looks like it gets to the last iteration of the for loop and then gets caught in an infinite loop.. Anyone know why?
infinite loop. The only provided loop (for loop over thecharArraydoes seem to end well. Other than that code is obviously wrong.