java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode current = head;
while (current!=null&¤t.next!=null){
if (current.val == current.next.val){
ListNode node = current.next;
current.next = node.next;
//释放空节点
node.next = null;
}else
{
current = current.next;
}
}
return head;
}
}
python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
res = head
while res and res.next:
if res.val==res.next.val:
res.next = res.next.next
else:
res = res.next
return head