Given a sorted linked list
, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5Output: 1->2->5
Example 2:
Input: 1->1->1->2->3Output: 2->3
思路: 快慢指针
, 用快指针跳过有重复值的链表, 慢指针负责和快指针拼接! 思路比较精妙, 后面值得二刷。
s cur/q // s: slow; q: quick1 -> 1 -> 1 -> 2 -> 3..s->cur q1 -> 1 -> 1 -> 2 -> 3..nexts --------------->cur/q1 -> 1 -> 1 -> 2 -> 3..s cur/q1 -> 1 -> 1 -> 2 -> 3..s1 -> 1 -> 1 -> 2 -> 3
/*** Definition for singly-linked list.* function ListNode(val) {* this.val = val;* this.next = null;* }*//*** @param {ListNode} head* @return {ListNode}*/var deleteDuplicates = function(head) {const dummyHead = new ListNode(0)dummyHead.next = headlet slowPoint = dummyHeadwhile(slowPoint.next) {let cur = slowPoint.nextlet quickPoint = curwhile(quickPoint.next && quickPoint.next.val === cur.val) {quickPoint = quickPoint.next}if (cur === quickPoint) {slowPoint = slowPoint.next} else {slowPoint.next = quickPoint.next}}return dummyHead.next}