leetcode_83. Remove Duplicates from Sorted List 删除单链表中的重复节点

来源:互联网 发布:linux实时守护进程 编辑:程序博客网 时间:2024/04/27 00:32

题目:

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.


题意:

删除单链表中重复的节点


代码:

# 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
        """
        
        cur_node = ListNode(0)
        cur_node = head
        
        while cur_node != None and cur_node.next != None :
            if cur_node.val == cur_node.next.val :
                cur_node.next = cur_node.next.next
            else :
                cur_node = cur_node.next
        return head



0 0
原创粉丝点击