在一个排序的链表中,存在重复的节点,请删除该链表中重复的节点,重复的节点不保留。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* deleteDuplication(ListNode* head) { if (!head) return NULL; auto dummy = new ListNode(-1); dummy->next = head; auto p = dummy; while (p->next) { auto q = p->next->next; while (q && q->val == p->next->val) q = q->next; if (q == p->next->next) p = p->next; else p->next = q; } return dummy->next; } };
标签:dummy,ListNode,删除,val,next,链表,节点 From: https://www.cnblogs.com/leetothemoon/p/16982017.html