力扣:83. 删除排序链表中的重复元素
This commit is contained in:
parent
142a168de7
commit
811bd71ed9
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user