题解 | 删除有序链表中重复的元素-I | 一次遍历

删除有序链表中重复的元素-I

http://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79

链表是升序排列,一次遍历只需比较和前一个节点是否相等,相等就删除

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

#
# 
# @param head ListNode类 
# @return ListNode类
#
class Solution:
    def deleteDuplicates(self , head ):
        # write code here
        dump = ListNode(0)
        dump.next = head
        while head and head.next:
            if head.val == head.next.val:
                head.next = head.next.next
                continue
            head = head.next
        return dump.next
          
全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务