*Output: 1->2 *Example 2: * *Input: 1->1->2->3->3 *Output: 1->2->3 **********************************************************************************/ ''' from SLinkedList import SLinked_List def deleteDuplicates(head): """ :type head: ListNode :rtype: ListNode """ current=head while current != None and current.next != None: if current.val == current.next.val: current.next=current.next.next else: current=current.next return head elements=[1,1,2,2,3,3,3] sll=SLinked_List() sll.add_elements(elements) print(sll) deleteDuplicates(sll.head) print(sll)
*Output: [4,1,9] *Explanation: You are given the second node with value 5, the linked list * should become 4 -> 1 -> 9 after calling your function. *Example 2: * *Input: head = [4,5,1,9], node = 1 *Output: [4,5,9] *Explanation: You are given the third node with value 1, the linked list * should become 4 -> 5 -> 9 after calling your function. **********************************************************************************/ ''' from SLinkedList import SLinked_List def deleteNode(node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ node.val = node.next.val node.next = node.next.next elements = [1, 2, 3, 4, 5] sll = SLinked_List() sll.add_elements(elements) print(sll) deleteNode(sll.search(4)) print(sll)