我想使用递归从已排序的链表中删除重复元素。任何人都可以帮助我
public void removeDuplicates(Node head) {
Node temp = head;
while (temp.next != null) {
if (temp.data.equals(temp.next.data)) {
Node next_next = temp.next.next;
temp.next = null;
temp.next = next_next;
} else {
temp = temp.next;
}
}
new LinkedListStack().iterateLinkedList(head);
return;
}
答案 0 :(得分:1)
// try this
public ListNode removeDuplicateElements(ListNode head) {
if (head == null || head.next == null) {
return null;
}
if (head.data.equals(head.next.data)) {
ListNode next_next = head.next.next;
head.next = null;
head.next = next_next;
removeDuplicateElements(head);
} else {
removeDuplicateElements(head.next);
}
return head;
}
答案 1 :(得分:0)
检查此代码。也许这可能会有所帮助。
ng-blur="count = !count"