LeetCode 83. 删除排序链表中的重复元素(链表)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 83. 删除排序链表中的重复元素(链表)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 題目
給定一個(gè)排序鏈表,刪除所有重復(fù)的元素,使得每個(gè)元素只出現(xiàn)一次。
示例 1: 輸入: 1->1->2 輸出: 1->2示例 2: 輸入: 1->1->2->3->3 輸出: 1->2->3來(lái)源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
2. 解題
class Solution { public:ListNode* deleteDuplicates(ListNode* head) {if(!head || !head->next)//個(gè)數(shù)小于2return head;ListNode *cur = head, *nt = head->next;while(nt){if(cur->val == nt->val)nt = nt->next;//找到不一樣的valelse{cur->next = nt;cur = nt;nt = nt->next;}}cur->next = NULL;//1-2-3-3-3,最后一次cur->next = NULLreturn head;} };我的CSDN博客地址 https://michael.blog.csdn.net/
長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!
總結(jié)
以上是生活随笔為你收集整理的LeetCode 83. 删除排序链表中的重复元素(链表)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LeetCode 1376. 通知所有员
- 下一篇: LeetCode 1368. 使网格图至