LeetCode 83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL || head->next == NULL) {
return head;
}
ListNode *p = head;
while(p != NULL && p->next != NULL) {
while (p->next != NULL && p->next->val == p->val) {
p->next = p->next->next;
}
p = p->next;
}
return head;
}
};
相關文章
- Remove-duplicates-from-sorted-listREM
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- Remove-duplicates-from-sorted-arrayREM
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM
- Leetcode(Python3) 19. Remove Nth Node From End of ListLeetCodePythonREM
- Leetcode Remove Duplicates型別題目 (python)LeetCodeREM型別Python
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- 資料結構與演算法 | Leetcode 19. Remove Nth Node From End of List資料結構演算法LeetCodeREM
- Java for LeetCode 109 Convert Sorted List to Binary Search TreeJavaLeetCode
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode
- leetcode-019-刪除連結串列倒數第N個結點(Remove Nth Node From End of List)LeetCodeREM
- Merge Two Sorted List
- 【leetcode】19. 刪除連結串列的倒數第N個節點(remove-nth-node-from-end-of-list)(雙指標)[中等]LeetCodeREM指標
- [leetcode]remove-elementLeetCodeREM
- Java List的remove()方法陷阱JavaREM
- Leetcode 442. Find All Duplicates in an ArrayLeetCode
- leetcode-27. Remove ElementLeetCodeREM
- Leetcode 27 Remove-ElementLeetCodeREM
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- 【程式碼優化】List.remove() 剖析優化REM
- [LeetCode] 402. Remove K DigitsLeetCodeREMGit
- Leetcode 88. Merge Sorted ArrayLeetCode
- Leetcode 23 Merge k Sorted ListsLeetCode
- Leetcode 21 Merge Two Sorted ListsLeetCode
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode 4 Median of Two Sorted ArraysLeetCode
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- SciTech-Mathmatics-ImageProcessing-Remove the Background from an image using Python?REMPython
- [LeetCode] 3011. Find if Array Can Be SortedLeetCode
- [LeetCode] Find First and Last Position of Element in SortedLeetCodeAST