题目:删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为1\to1\to21→1→2,返回1 \to 21→2.
给出的链表为1\to1\to 2 \to 3 \to 31→1→2→3→3,返回1\to 2 \to 31→2→3.
提示:
[0, 300]
内-100 <= Node.val <= 100
思路:
代码:
1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val, next) { 4 * this.val = (val===undefined ? 0 : val) 5 * this.next = (next===undefined ? null : next) 6 * } 7 */ 8 /** 9 * @param {ListNode} head 10 * @return {ListNode} 11 */ 12 var deleteDuplicates = function(head) { 13 let current = head; 14 while(current !== null && current.next !== null){ 15 if(current.val === current.next.val){ 16 current.next = current.next.next; 17 }else{ 18 current = current.next; 19 } 20 } 21 return head; 22 };
原文:https://www.cnblogs.com/icyyyy/p/14800334.html