Simple and easily understanding Python solution


  • 0
    F
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
    # @param {ListNode} head
    # @return {ListNode}
    def deleteDuplicates(self, head):
        if head==None or head.next==None:
            return head
        a=[]
        node=head
        while node is not None:
            a.append(node.val)
            node=node.next
        head=None
        node1=None
        node2=None
        for i in a:
            if a.count(i)<=1:
                if head is None:
                    node2=ListNode(i)
                    head=node2
                    node1=node2
                else:
                    node2=ListNode(i)
                    node1.next=node2
                    node1=node1.next
        return head

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.