Problem
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
too young 思路
每一个数字都和上一个数字进行比较,如果一样的话就让上一个数字直接指向下一个数字,但是我不知道语法。。
dalao思路-28ms
这次的思路是对的,用小本本记下来
新知识get:
In Python, every value is a reference, you can say a pointer, to an object. Objects cannot be values. Assignment always copies the value; two such pointers point to the same object.
# 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):
cur = head
while cur:
while cur.next and cur.next.val == cur.val:
cur.next = cur.next.next
cur = cur.next
return head