描述
给出一条链表,删除重复的节点
解决
直接遍历
/**
* 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) {
char buf[sizeof(ListNode)];
ListNode* ptrHead = new(buf) ListNode(-1);
ptrHead -> next = head;
while (head){
ListNode* node = head ? head -> next : NULL;
//cout << "zz" << head -> val << endl;
int tmpVal = node ? node -> val : 0x3f3f3f;
if (tmpVal == head -> val){
head -> next = node ? node -> next : NULL;
continue;
}
head = head -> next;
}
return ptrHead -> next;
}
};