题解 | #牛群排列去重#
牛群排列去重
https://www.nowcoder.com/practice/8cabda340ac6461984ef9a1ad66915e4
知识点:
链表操作/双指针
分析:
两个指针:
设置dummy节点是用来返回最后的整个链表:return dummy->next;
从i开始遍历
i 和 j不等于的时候 i和j一起往后移动
i 和 j等于的时候就是重复了,那么再循环内,使用一个临时的节点来接住 j->next这个节点,让i和j后面的节点直接相连接。
i->next 为空的时候停止遍历。
编程语言:
C++
完整代码:
ListNode* deleteDuplicates(ListNode* head) { // write code here if(head == nullptr) return head; ListNode* dummy = new ListNode(0); dummy->next = head; ListNode* i = head; ListNode* j = head->next; while(i->next){ if(i->val == j->val){ ListNode* k = j->next; i->next = k; j = k; }else{ i = i->next; j= j->next; } } return dummy->next; }