C++:
struct ListNode {
public:
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode *previousNode = NULL;
ListNode *currentNode = head;
while ( currentNode ) {
if ( !previousNode ) {
previousNode = currentNode;
currentNode = currentNode -> next;
} else if ( previousNode -> val != currentNode -> val ) {
previousNode = currentNode;
currentNode = currentNode -> next;
} else if ( previousNode -> val == currentNode -> val ) {
ListNode *nextNode = currentNode -> next;
delete currentNode;
currentNode = nextNode;
previousNode -> next = currentNode;
}
}
return head;
}
};
网友评论