82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
1、非递归思路(不带头结点):
这种方法最难,因为没有头结点,要是一开始就有重复的就很难删除,不过可以借助标志位,一定要注意谢谢测试用例,比如 1 1 1 1 2 1 1 2 2 等
2、递归思路:
如果第一个结点和第二个结点不一样,则第一个结点可以直接作为新链表的头结点,后面继续删除重复的过程;如果第一个结点和第二个结点相同,则需要继续遍历,直到找到第一个与第一个结点不同的结点作为新链表的头结点,如果找不到,说明链表所有结点元素都一样,新链表为NULL,否则从新的头结点出发,继续删除重复的过程。
3、非递归思路:
新建链表头结点指针pDel,pDel->next=head,并设置指针prev指针指向pDel,curr指针指向head->next(代表遍历指针);当curr->next不为NULL,如果curr->next->val == curr->val,curr=curr->next;如果curr->next->val != curr->val;则需判断prev->next==curr?如果是,则prev=curr;如果不是,则prev->next=curr->next.(这里是说,prev先假设一个next指针,即curr=curr->next;当进行下一步判断时,如果curr->next->val != curr->val 且 prev->next==curr,则说明假设正确,prev直接指向curr)
1 class Solution { 2 public: 3 ListNode* deleteDuplicates(ListNode* head) { 4 int flag1 = 0, flag2 = 0; 5 if (head == NULL || head->next == NULL) 6 { 7 return head; 8 } 9 while (head->next != NULL && head->val == head->next->val) 10 { 11 head = head->next; 12 flag1 = 1; 13 } 14 if (head->next == NULL) 15 { 16 if (flag1 == 1) 17 { 18 head = head->next; 19 flag1 = 0; 20 } 21 return head; 22 } 23 24 ListNode *p = head; 25 ListNode *q = head->next->next; 26 27 while (q != NULL) 28 { 29 if (p->next->val == q->val) 30 { 31 p->next->next = q->next; 32 delete q; 33 q = p->next->next; 34 flag2 = 1; 35 } 36 else 37 { 38 if (flag2 == 1) 39 { 40 delete p->next; 41 p->next = q; 42 q = q->next; 43 flag2 = 0; 44 } 45 else 46 { 47 p = p->next; 48 q = q->next; 49 } 50 51 } 52 53 } 54 55 if (flag1 == 1 && flag2 == 0) 56 { 57 head = head->next; 58 flag1 = 0; 59 } 60 else if (flag2 == 1 && flag1 == 0) 61 { 62 p->next = NULL; 63 flag2 = 0; 64 } 65 else if (flag1 == 1 && flag2 == 1)//1 1 1 1 2 2 1 1 2 66 { 67 delete p->next; 68 p->next = NULL; 69 head = head->next; 70 flag1 = 0; 71 flag2 = 0; 72 } 73 74 return head; 75 76 } 77 };
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* deleteDuplicates(ListNode* head) { 12 if (head == NULL || head->next == NULL) 13 { 14 return head; 15 } 16 ListNode *p = head->next; 17 18 if (p->val != head->val) 19 { 20 head->next = deleteDuplicates(p); 21 return head; 22 } 23 else 24 { 25 while (p != NULL && p->val == head->val) 26 { 27 p = p->next; 28 } 29 return deleteDuplicates(p); 30 } 31 } 32 };
1 class Solution { 2 public: 3 ListNode* deleteDuplicates(ListNode* head) { 4 if(head==NULL || head->next==NULL) 5 return head; 6 7 ListNode *pDel=new ListNode(0); 8 pDel->next=head; 9 10 ListNode *prev=pDel; 11 ListNode *curr=prev->next; 12 13 while(curr->next){ 14 if(curr->next->val!=curr->val){ 15 if(prev->next==curr) 16 prev=curr; 17 else 18 prev->next=curr->next; 19 } 20 curr=curr->next; 21 } 22 23 if(prev->next!=curr) 24 prev->next=curr->next; 25 26 return pDel->next; 27 } 28 };