LeetCode: Remove Duplicates from Sorted List

来源:互联网 发布:网络赚钱平台 编辑:程序博客网 时间:2024/04/28 00:37

题目

https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/


分析

从前向后遍历即可


代码

class Solution{public:ListNode *deleteDuplicates(ListNode *head){if (head == NULL || head->next == NULL)return head;ListNode *p = head;while (p != NULL && p->next != NULL){if (p->val == p->next->val){ListNode *temp = p->next;p->next = p->next->next;delete temp;}elsep = p->next;}return head;}};



参考

http://www.programcreek.com/2013/01/leetcode-remove-duplicates-from-sorted-list/


0 0
原创粉丝点击