leetcode 83删除排序链表的重复元素

突然感觉自己对链表题还不是很熟,有点弄不清楚链表中间是怎么断链再重接的了

这道题目如下

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2
示例 2:

输入: 1->1->2->3->3
输出: 1->2->3
```

由于是一个已经排好序的链表,所以只需要判断,前一个节点和后一个节点的值是否相同就可以了,
但是中间我的断链断的我脑瓜子疼,现在差不多理解了,应该就是使用一个临时节点(相当于for i in range(10) 中的i ),然后遍历一个个节点,通过改变链表节点的next指向的对象来改变链表结构,代码如下。
```py
# 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
        """
        get_head = head
        # print(id(get_head),id(head))
        while get_head and get_head.next:
        	#这一步起辅助理解作用
            #print(get_head.val)
            if get_head.next.val == get_head.val:
                get_head.next = get_head.next.next
            else:
                get_head = get_head.next 
            # print(id(get_head),id(head))
        return head
```
原文地址:https://www.cnblogs.com/yfc0818/p/11072613.html