力扣:83. 删除排序链表中的重复元素

This commit is contained in:
huangge1199 2021-02-07 10:14:24 +08:00
parent 142a168de7
commit 811bd71ed9

View File

@ -0,0 +1,20 @@
package com.code.leet.study.t20210207;
import com.code.leet.entiy.ListNode;
/**
* 给定一个排序链表删除所有重复的元素使得每个元素只出现一次
*/
public class DeleteDuplicates {
public ListNode deleteDuplicates(ListNode head) {
ListNode temp = head;
while (temp != null && temp.next != null) {
if (temp.next.val == temp.val) {
temp.next = temp.next.next;
} else {
temp = temp.next;
}
}
return head;
}
}