题目:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
解法1:
迭代法
相比于83题将重复的元素只留一个,该题将重复的元素全部删去
所以head节点也有可能会被删去,所以要增加head节点前增加一个dummy节点
设置两个指针pre和cur,初始指向dummy和head节点
遍历链表,分两种情况,一种是未遇到重复节点,这种情况,每次pre节点被赋为当前节点cur,当前节点cur继续向后移动一位
另一种遇到重复节点,这样的话,一直遍历重复的节点知道最后一个重复的节点,最后通过改变pre指针的位置来忽略这些相同的节点
记得在while循环中加上 head->next不为空的判断条件
c++:
/**
* 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* dummy = new ListNode(-1);
dummy->next = head;
ListNode* pre = dummy;
ListNode* cur = head;
while(cur && cur->next){
bool equal = false;
while(cur->next && cur->val == cur->next->val){
cur = cur->next;
equal = true;
}
if(equal){
pre->next = cur->next;
} else {
pre = cur;
}
cur = cur->next;
}
return dummy->next;
}
};
java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = head;
while(cur!=null && cur.next!=null){
boolean equal = false;
while(cur.next!=null && cur.val == cur.next.val){
cur = cur.next;
equal = true;
}
if(equal){
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}
}
python:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy = ListNode(-1)
dummy.next = head
pre = dummy
cur = head
while cur and cur.next:
equal = False
while cur.next and cur.val == cur.next.val:
cur = cur.next
equal = True
if equal:
pre.next = cur.next
else:
pre = cur
cur = cur.next
return dummy.next
解法2:
递归法
相比于83题将重复的元素只留一个,该题将重复的元素全部删去,所以相比于83题的if条件增加了while循环
83题:如果两个节点相同,保留第二个节点
该题:如果两个节点相同,继续循环向后移动直到到达最后一样相同的节点,然后舍弃这个节点从它的下一个节点继续递归
记得在while循环中加上 head->next不为空的判断条件
c++:
/**
* 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) {
if(!head || !head->next){
return head;
}
if(head->val == head->next->val){
while(head->next && head->val == head->next->val){
head = head->next;
}
return deleteDuplicates(head->next);
}else{
head->next = deleteDuplicates(head->next);
}
return head;
}
};
java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null){
return head;
}
if(head.val == head.next.val){
while(head.next != null && head.val == head.next.val){
head = head.next;
}
return deleteDuplicates(head.next);
}else{
head.next = deleteDuplicates(head.next);
}
return head;
}
}
python:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next:
return head
if head.val == head.next.val:
while head.next and head.val == head.next.val:
head = head.next
return self.deleteDuplicates(head.next)
else:
head.next = self.deleteDuplicates(head.next)
return head