LeetCode 83. Remove Duplicates from Sorted List

来源:互联网 发布:全国中小学名录数据库 编辑:程序博客网 时间:2024/06/02 06:28

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.

分析:

题目大意为将给定的已排序好的数组删除重复的数组,若当前的地址的值与它所指的地址的值是相同的,则将这个地址的指针指向下下个地址

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

阅读全文
2 0
原创粉丝点击