Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Return the linked list sorted as well.
Input: 1->2->3->3->4->4->5 Output: 1->2->5
Input: 1->1->1->2->3 Output: 2->3
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# @param {ListNode} head
# @return {ListNode}
def delete_duplicates(head)
dummy = ListNode.new(0, head)
curr = dummy
until curr.nil?
if !curr.next.nil? && !curr.next.next.nil? && curr.next.val == curr.next.next.val
curr.next.next = curr.next.next.next while !curr.next.next.nil? && curr.next.val == curr.next.next.val
curr.next = curr.next.next
else
curr = curr.next
end
end
dummy.next
end