LeetCode: Remove Duplicates from Sorted List

题目

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;
			}
			else
				p = p->next;
		}
		return head;
	}
};

参考

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

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/39456291

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();