close
我也不知道為什麼Leetcode這麼喜歡出linklist, 我印象中這種題目我做過不知道幾十題去了, 話說到了我這個年紀, 刷刷這種題目的目的真的是只剩下醒腦了, 不會像是以前一樣為了去面試而準備了, 嘖嘖, 這算是一種進步了嗎?
不過講到這個, 人家說年紀真的會影響刷題的速度... , 但我好像是早上有沒有喝咖啡來決定我有沒有辦法開機...;
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:
Input: head = [1,1,2] Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3] Output: [1,2,3]
Constraints:
- The number of nodes in the list is in the range
[0, 300]
. -100 <= Node.val <= 100
- The list is guaranteed to be sorted in ascending order.
ListNode* deleteDuplicates(ListNode* head) {
if (head == NULL) {
return NULL;
}
ListNode *temp = head;
ListNode *nextPtr;
while (temp->next != NULL) {
if (temp->val == temp->next->val) {
nextPtr = temp->next->next;
delete(temp->next);
temp->next = nextPtr;
}
else {
temp = temp->next;
}
}
return head;
}
全站熱搜
留言列表