LeetCode 刷题
删除排序链表中的重复元素
题目描述:给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次.返回已排序的链表
图解 :
代码:
public class ListNode {
int val;
ListNode() {}
ListNode(int val) { this.val = val;}
ListNode(int val,ListNode next) {this.val = val;this.next = next;}
}
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode cur = head;
while (cur.next != null) {
if(cur.val == cur.next.val) {
cur.next = cur.next.next;
}else {
cur = cur.next;
}
}
return head;
}
}