题目
思路
- 思路一:循环遍历链表,如果出现重复则跳过该值
-
代码
public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) return head;ListNode cur = head, nex = head.next;while (nex != null) {if (cur.val == nex.val) {cur.next = nex.next;nex = nex.next;} else {cur = cur.next;nex = nex.next;}}return head;}public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) return head;head.next = deleteDuplicates(head.next);if (head.val == head.next.val) head = head.next;return head;}
