[leetcode] 83. Remove Duplicates from Sorted List

原创 2016年06月02日 11:39:11

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.

Solution:

Compare the value of current node to the value of next node. 

 /**
 * 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) {
        if (!head) return head;
        ListNode* cur = head;
        while (cur->next){
            if (cur->val != cur->next->val)
                cur = cur->next;
            else{
                cur->next = cur->next->next;
            }
        }
        return head;
        
    }
};




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode_83_Remove Duplicates from Sorted List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41728739 Given a sorted linked...

LeetCode 83 — Remove Duplicates from Sorted List(C++ Java Python)

题目:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ Given a sorted linked list, ...

LeetCode.83. Remove Duplicates from Sorted List(删除单链表中的重复元素)

Given a sorted linked list, delete all duplicates such that each element appear only once. For exam...

LeetCode 83 - Remove Duplicates from Sorted List

Remove Duplicates from Sorted List题目链接 https://leetcode.com/problems/remove-duplicates-from-sorted...
  • calmnea
  • calmnea
  • 2015年09月06日 21:18
  • 198

Leetcode NO.83 Remove Duplicates from Sorted List

本题的题目要求如下: Given a sorted linked list, delete all duplicates such that each element appear only on...

leetcode 83 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For examp...

LeetCode - 83. Remove Duplicates from Sorted List

方法一: 这种方法是自己想起来的,只用到了一个指针,因此相较于使用两个指针的情况,处理时要来的麻烦些。使用两个指针的方法简便易行,所以在实际中还是比较推荐第二种,但是这种方法仍有些需要注意之处。首先...

LeetCode笔记:83. Remove Duplicates from Sorted List

删除排好序的链表中重复的节点

LeetCode(83)Remove Duplicates from Sorted List

题目如下: Given a sorted linked list, delete all duplicates such that each element appear only once. For...

LeetCode 83 Remove Duplicates from Sorted List(从已排序链表中移除重复元素)(Linked List)(*)

翻译给定一个已排序链表,删除所有重复元素让每个元素只出现一次。例如: 给定 1->1->2, 返回 1->2。 给定 1->1->2->3->3, 返回 1->2->3。原文Given a sorte...
  • NoMasp
  • NoMasp
  • 2016年01月14日 08:31
  • 1725
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 83. Remove Duplicates from Sorted List
举报原因:
原因补充:

(最多只允许输入30个字)