LeetCode Algorithm 83. 删除排序链表中的重复元素
生活随笔
收集整理的這篇文章主要介紹了
LeetCode Algorithm 83. 删除排序链表中的重复元素
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
83. 刪除排序鏈表中的重復元素
Ideas
這題挺簡單的,直接一次遍歷,如果當前遍歷的元素val跟下一個元素的val相等,說明是重復元素,直接把當前item的next指向item->next->next。
Code
C++
class Solution { public:ListNode* deleteDuplicates(ListNode* head) {if (!head) {return head;}ListNode* item = head;while (item->next) {if (item->val == item->next->val) {item->next = item->next->next;} else {item = item->next;}}return head;} };總結
以上是生活随笔為你收集整理的LeetCode Algorithm 83. 删除排序链表中的重复元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode Algorithm 9
- 下一篇: 将森林转换为对应的二叉树,若在二叉树中,