关闭

Remove Duplicates from Sorted List

标签: list
394人阅读 评论(0) 收藏 举报
分类:

题目描述

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

Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

链接地址

http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list/

解法

     ListNode *deleteDuplicates(ListNode *head) {
        // write your code here
        if (head == NULL) {
            return head;
        }
        ListNode *dummyNode = head;
       // dummyNode = head;
        while (dummyNode != NULL) {
            if (dummyNode->next != NULL && dummyNode->val == dummyNode->next->val) {
                ListNode *temp = dummyNode->next;
                dummyNode->next = dummyNode ->next->next;
                delete temp;
            } else {
                dummyNode = dummyNode->next;
            }
        }
        return head;
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:319456次
    • 积分:5123
    • 等级:
    • 排名:第5392名
    • 原创:196篇
    • 转载:4篇
    • 译文:0篇
    • 评论:11条
    最新评论