82. Remove Duplicates from Sorted List II(js)
Posted mingL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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->5 Output: 1->2->5
Example 2:
Input: 1->1->1->2->3 Output: 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; };
以上是关于82. Remove Duplicates from Sorted List II(js)的主要内容,如果未能解决你的问题,请参考以下文章
82. Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List II(js)
82. Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List II