Leetcode82. 删除排序链表中的重复元素 II
题目描述:
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
实例:
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
解法1:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
new = ListNode(None)
prev = new
while head:
value = head.val
if not head.next or head.next.val != value:
prev.next = head
prev = head
head = head.next
continue
while head.next and head.next.val == value:
head = head.next
head = head.next
prev.next = None
return new.next