82. Remove Duplicates from Sorted List II(js)

82. Remove Duplicates from Sorted List II

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
题意:给定一个链表,删除所有有重复值的节点,返回这个链表
代码如下:
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} head * @return {ListNode} */var deleteDuplicates = function(head) { if(!head || !head.next) return head; let start=new ListNode(0); start.next=head; let pre=start; while(pre.next){ let cur=pre.next; while(cur.next && cur.next.val===cur.val) cur=cur.next; if(cur!=pre.next) pre.next=cur.next; else pre=pre.next; } return start.next;};

 

相关文章