public LinkedIntList splitBySign() {
LinkedIntList res = new LinkedIntList();
ListNode back = res.front;
while (front != null && front.data < 0) {
ListNode temp = front;
front = front.next;
temp.next = null;
if (back == null) {
res.front = temp;
} else {
back.next = temp;
back = back.next;
}
}
ListNode curr = front;
while (curr != null && curr.next != null) {
if (curr.next.data < 0) {
ListNode temp = curr.next;
curr.next = temp.next;
temp.next = null;
if (back == null) {
res.front = temp;
} else {
back.next = temp;
back = back.next;
}
} else {
curr = curr.next;
}
}
return res;
}
This method is intended to split a LinkedIntList so that all negative values will be removed to another LinkedIntList and finally return the created LinkedIntList. However when I actually run this code, the test List is [-1, -1, -2, -3, 3, 0, 1, 2, 3], the change of the original list is the expected [3, 0, 1, 2, 3], but the returned list is [-3] instead of [-1, -1, -2, -3].