删除排序链表中的重复元素 II
题目介绍
存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。
返回同样按升序排列的结果链表。
示例1:

示例 2:

提示:
- 链表中节点数目在范围
[0, 300] 内
-100 <= Node.val <= 100
- 题目数据保证链表已经按升序排列
题目解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| package algorithm;
public class RemoveDuplicatesFromSortedListII {
public static ListNode deleteDuplicates(ListNode head) { if (head == null) { return head; }
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy; while (cur.next != null && cur.next.next != null) { if (cur.next.val == cur.next.next.val) { int x = cur.next.val; while (cur.next != null && cur.next.val == x) { cur.next = cur.next.next; } } else { cur = cur.next; } }
return dummy.next; }
public static void main(String[] args) { ListNode n1 = new ListNode(1); ListNode n2 = new ListNode(2); n1.next = n2; ListNode n3 = new ListNode(3); n2.next = n3; ListNode n4 = new ListNode(3); n3.next = n4; ListNode n5 = new ListNode(4); n4.next = n5; ListNode n6 = new ListNode(4); n5.next = n6; ListNode n7 = new ListNode(5); n6.next = n7; print(deleteDuplicates(n1));
n1 = new ListNode(1); n2 = new ListNode(1); n1.next = n2; n3 = new ListNode(1); n2.next = n3; n4 = new ListNode(2); n3.next = n4; n5 = new ListNode(3); n4.next = n5; print(deleteDuplicates(n1));
n1 = new ListNode(1); n2 = new ListNode(1); n1.next = n2; print(deleteDuplicates(n1)); }
public static class ListNode { int val; ListNode next;
ListNode() { }
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; } }
private static void print(ListNode head) { while (head != null) { System.out.print(head.val + " "); head = head.next; } System.out.println(); } }
|
打印:
思路:
思路很简单。就是加一个哑节点。注意,不要思考过于复杂。