[LeetCode] 83. 删除排序链表中的重复元素
题目链接 : https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
题目描述:
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例:
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
思路:
思路一:迭代,快慢指针,更容易理解
思路二:递归
自己看代码,很好理解!
相关题目:82. 删除排序链表中的重复元素 II
代码:
思路一:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
dummy = ListNode(-1000)
dummy.next = head
slow = dummy
fast = dummy.next
while fast :
if slow.val == fast.val:
fast = fast.next
slow.next = fast
else:
slow = slow.next
fast = fast.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(-1000);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = dummy.next;
while (fast != null) {
if (slow.val == fast.val) {
fast = fast.next;
slow.next = fast;
} else {
slow = slow.next;
fast = fast.next;
}
}
return dummy.next;
}
}
思路二
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head: return head
if head.next and head.val == head.next.val:
while head.next != None and head.val == head.next.val:
head = head.next
return self.deleteDuplicates(head)
else:
head.next = self.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) return head;
if (head.next != null && head.val == head.next.val) {
while (head.next != null && head.val == head.next.val) {
head = head.next;
}
return deleteDuplicates(head);
}
else head.next = deleteDuplicates(head.next);
return head;
}
}