/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */
publicclassSolution { public ListNode DeleteDuplicates(ListNode head) { if (head == null) return head;
ListNode first = head.next; ListNode second = head; while (first != null) { if (first.val == second.val) second.next = first.next; else second = second.next; first = first.next; } return head; } }
Python 语言
执行结果:通过
执行用时:52 ms, 在所有 Python3 提交中击败了 33.88% 的用户
内存消耗:13.5 MB, 在所有 Python3 提交中击败了 12.75% 的用户
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: defdeleteDuplicates(self, head: ListNode) -> ListNode: if head isNone: return head
first = head.next second = head while first isnotNone: if first.val == second.val: second.next = first.next else: second = second.next first = first.next return head