0%

Javascript(leetcode#83) Remove Duplicates from Sorted List

Difficult:Easy

題目

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

翻譯

給定排序鍊錶的頭部,刪除所有重複項,使每個元素只出現一次。返回排序好的鍊錶。

範例

Example 1:
example

1
2
Input: head = [1,1,2]
Output: [1,2]

Example 2:
example

1
2
Input: head = [1,1,2,3,3]
Output: [1,2,3]

解題思路

1.設當前及存放開頭的變數
2.當遇到相同值時,依序往前比較

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var deleteDuplicates = function (head) {
let cur = head;
let headnode = head;
while (cur != null&&cur.next!=null) {
let curval = cur.val;
if (curval == cur.next.val) {
let next = cur.next;
while (next != null && next.val == curval) {
next = next.next;
}
cur.next=next;
cur=next;
}else{
cur=cur.next;
}
}
return headnode;
};