题解 | #删除有序链表中重复的元素-I#

删除有序链表中重复的元素-I

https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79

//写代码写懵了,忘了通过val值来判断
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
		ListNode* nList = new ListNode(999);
		nList->next = head;
		ListNode* Last = nList;
		ListNode* quick=head, * slow=nList;
		while (head==NULL)
		{
			return NULL;
		}
		while (quick->next!=NULL)
		{
			if (quick->val == slow->val)
			{
				quick = quick->next;
				slow->next = quick;
			}
			else
			{
				quick = quick->next;
				slow = slow->next;
			}
		}
		if (slow->val == quick->val)
			slow->next = NULL;
		return nList->next;
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务