Remove Duplicates from Sorted List 去除链表中重复值节点
生活随笔
收集整理的這篇文章主要介紹了
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.
如題目所訴,去除遞增鏈表中重復值的節點。
剛開始思路如下:
但是會有個問題,如果是{1,1,1},那么遍歷到第一個1的時候,判斷和第二個1相同,則將第一個1的next指向第三個1.
程序結束,最后輸出為{1,1},所以此方法行不通。
/*** 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) {ListNode *node = head;while(NULL != node && NULL != node->next) {if(node->next->val == node->val) {node->next = node->next->next;} else {node = node->next;}}return head;} };
轉載于:https://www.cnblogs.com/fanchangfa/p/4058229.html
總結
以上是生活随笔為你收集整理的Remove Duplicates from Sorted List 去除链表中重复值节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [我给Unity官方视频教程做中文字幕]
- 下一篇: 计划任务设计注意