# 删除排序链表中的重复元素

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

返回同样按升序排列的结果链表。

 

示例 1:

输入:head = [1,1,2]
输出:
[1,2]

示例 2:

输入:head = [1,1,2,3,3]
输出:
[1,2,3]

 

提示:

## template ```python class ListNode(object): def __init__(self, x): self.val = x self.next = None class LinkList: def __init__(self): self.head=None def initList(self, data): self.head = ListNode(data[0]) r=self.head p = self.head for i in data[1:]: node = ListNode(i) p.next = node p = p.next return r def convert_list(self,head): ret = [] if head == None: return node = head while node != None: ret.append(node.val) node = node.next return ret class Solution(object): def deleteDuplicates(self, head): if head is None: return None pos = head while pos is not None and pos.next is not None: if pos.val == pos.next.val: pos.next = pos.next.next else: pos = pos.next return head # %% l = LinkList() list1 = [1,1,2] l1 = l.initList(list1) s = Solution() print(l.convert_list(s.deleteDuplicates(l1))) ``` ## 答案 ```python ``` ## 选项 ### A ```python ``` ### B ```python ``` ### C ```python ```