83. Remove Duplicates from Sorted List

I am not sure why my solution fails to linked list question fail. My initial thought was to use two pointers along with a dummy node. The p1 start at dummy node while p2 starts at the head; they both iterate until they find a duplicate node and remove it from the list. The error message shows it still shows process exited with signal SIGILL. How do I resolve this?

func deleteDuplicates(_ head: ListNode?) -> ListNode? {  
        var dummyNode = ListNode(0, head)
        var p1: ListNode? = dummyNode
        var p2 = head
      
        while  p1 != nil && p2 != nil{
            if p1!.val != p2!.val {
                p1 = p1!.next
                p2 = p2!.next
            }
            else if p1!.val == p2!.val{
                p2!.next = p2?.next!.next 
            }
        }
        return p2
    }
Terms of Service

Privacy Policy

Cookie Policy