存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 。返回同样按升序排列的结果链表。
public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) { return head; } ListNode temp = head; while (temp.next != null) { if (temp.next.val == temp.val) { temp.next = temp.next.next; } else { temp = temp.next; } } return head; }
原文:https://www.cnblogs.com/init-qiancheng/p/14584054.html