删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为,返回.
给出的链表为,返回.
例如:
给出的链表为,返回.
给出的链表为,返回.
数据范围:链表长度满足 ,链表中任意节点的值满足
进阶:空间复杂度 ,时间复杂度
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // 维护住循环不定式(loop invariant) // 从头指针指向的节点到curr指向的节点之间不存在重复的元素! ListNode* curr = head; while (curr && curr->next) { if (curr->val == curr->next->val) { ListNode* node_to_delete = curr->next; curr->next = curr->next->next; delete node_to_delete; } else { curr = curr->next; } } return head; } };
/** * 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) { ListNode * p=head; while(p!=NULL&&p->next!=NULL) { ListNode * q=p->next; while(q!=NULL&&q->val==p->val) { q=q->next; } p->next=q; p=q; } return head; } };
class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *p = head,*q; while(p != NULL && p->next != NULL) { q = p->next; if(q->val == p->val) { p->next = q->next; }else{ p->next = q; p = p->next; } } return head; } };
public ListNode deleteDuplicates(ListNode head) { ListNode cur = head; while(cur != null){ while(cur.next != null && cur.val == cur.next.val){ cur.next = cur.next.next; } cur = cur.next; } return head; }
public class Solution { public ListNode deleteDuplicates (ListNode head) { ListNode tou = head; while(head != null && head.next != null) { if(head.val == head.next.val) { head.next = head.next.next; } else { head = head.next; } } return tou; } }一个while搞定得了。重复的就不往后走,一直删。不重复指针在往后移动一位即可。
public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { if(head==null||head.next==null)return head; ListNode cur=head; while(cur!=null&&cur.next!=null){ if(cur.next.val==cur.val){ cur.next=cur.next.next; } else cur=cur.next; } return head; } }
/* * 目的:删除链表中重复元素,只保留一次 * 方法:新建一个头节点, * 如果当前节点与next节点的值相等,则分别移至下一个节点, * 否则插入到新的链表中,最后返回dummy.next */ ListNode *deleteDuplicates(ListNode *head) { if (head == nullptr) return head; ListNode dummy(-1); ListNode*pre = &dummy; ListNode*next =nullptr; while(head){ next = head->next; if (next==nullptr || head->val != next->val) { head->next = nullptr; pre->next = head; pre=pre->next; } head=next; } return dummy.next; }
//要断开无用的指针,防止内存泄露 class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode* p1 = head; while(p1 != NULL && p1->next != NULL) { ListNode* p2 = p1->next; if(p1->val != p2->val) { p1->next = p2; p1 = p1->next; continue; } while(p2->next != NULL && p1->val == p2->next->val) p2 = p2->next; p1->next = p2->next; delete p2; p1 = p1->next; } return head; } };
struct ListNode* deleteDuplicates(struct ListNode* head ) { struct ListNode *res, *p=head->next; if(head==NULL || head->next==NULL) return head; res = deleteDuplicates(head->next); while(p!=NULL) { if(p->val == head->val) return res; p = p->next; } head->next = res; return head; }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { ListNode dummy=new ListNode(0); dummy.next=head; ListNode node=dummy; while(node.next!=null&&node.next.next!=null){ if(node.next.val==node.next.next.val){ node=node.next; int x=node.next.val; while(node.next!=null&&node.next.val==x){ node.next=node.next.next; } } else node=node.next; } return dummy.next; } }
如果要删除 所有存在重复的元素
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { ListNode dummy=new ListNode(0); dummy.next=head; ListNode node=dummy; while(node.next!=null&&node.next.next!=null){ if(node.next.val==node.next.next.val){ //node=node.next; 把这一行注释掉即可。 int x=node.next.val; while(node.next!=null&&node.next.val==x){ node.next=node.next.next; } } else node=node.next; } return dummy.next; } }
class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { if (head == nullptr) return head; ListNode* cur = head; ListNode* nex = head->next; while(nex){ if(cur->val == nex->val){ cur->next = nex->next; }else{ cur = nex; } nex = nex->next; } return head; } };
class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here if(head == NULL) return head; ListNode *cur = NULL, *list = head,*next = NULL; while(list){ while(list->val == list->next->val && list->next != NULL){ cur = list->next->next; list->next->next = NULL; list->next = cur; } list = list->next; } return head; } };
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here if(head==null||head.next==null) return head; ListNode pre=head; ListNode cur=head.next; while(cur!=null){ if(pre.val==cur.val){ pre.next=cur.next; cur=pre.next; } else{ pre=cur; cur=cur.next; } } return head; } }
//熟悉一下ListNode的 next val node class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode node = head; while(node != null && node.next != null){ if(node.val == node.next.val){ node.next = node.next.next; } else{ node=node.next; } } return head; } }