【leetcode】83-Remove Duplicates from Sorted List

problem

Remove Duplicates from Sorted List

The basic usage of linked list is investigated.

code

/**

* 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
* cur = head;
while(cur && cur->next)//null is error.
{
if(cur->val == cur->next->val) cur->next = cur->next- >next;
else cur = cur->next; //
}
return head;
}
};

Reference

1. Remove Duplicates from Sorted List ;

End

/**

* 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
* cur = head;
while(cur && cur->next)//null is error.
{
if(cur->val == cur->next->val) cur->next = cur->next- >next;
else cur = cur->next; //
}
return head;
}
};

Leave a Comment

Your email address will not be published.