[LeedCode OJ] #83 Remove Duplicates from Sorted List,
[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]
Link: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
Question:
A sorted linked list requires that all repeated elements in it be removed.
Ideas:
Because the linked list itself is ordered, you only need to determine whether the current node value is equal to the previous value.
/** * 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==nullptr||head->next==nullptr)return head;ListNode *node = new ListNode(head->val);ListNode *newlist = node;ListNode *cur = head;while(cur!=nullptr){if(cur->val!=newlist->val){newlist->next = new ListNode(cur->val);newlist = newlist->next;}cur = cur->next;}return node;}};
Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source