Difficult:Medium
題目
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
翻譯
給定一個排序鍊錶的頭部,刪除所有具有重複數字的節點,只留下原始鍊錶中不同的數字。返回排序好的鍊錶。
範例
Example 1:
1 | Input: head = [1,2,3,3,4,4,5] |
Example 2:
1 | Input: head = [1,1,1,2,3] |
解題思路
1.設當前及上一個node和存放開頭的變數
2.當遇到相同值時,依序往前比較
3.如pre還是空時,將開頭直接等於next
Solution
1 | var deleteDuplicates = function (head) { |