83. 删除排序链表中的重复元素
这里和数组去重不同,数组去重是先让slow前进再给slow赋值,链表是先给slow赋值,再让slow前进一格。
ListNode deleteDuplicates(ListNode head) {if (head == null) return null;ListNode slow = head, fast = head;while (fast != null) {if (fast.val != slow.val) {// nums[slow] = nums[fast];slow.next = fast;// slow++;slow = slow.next;}// fast++fast = fast.next;}// 断开与后面重复元素的连接slow.next = null;return head;}
