82.删除排序链表中的重复元素II
文章来源:https://www.toymoban.com/news/detail-740700.html
由于链表是排好序的,所以只需要对其进行一次遍历即可,比较相邻节点对应的值文章来源地址https://www.toymoban.com/news/detail-740700.html
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* 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) {
ListNode dumyHead = new ListNode(0);
dumyHead.next = head;
ListNode cur = dumyHead;
while(cur.next != null && cur.next.next != null){
if(cur.next.val == cur.next.next.val){
int x = cur.next.val;
while(cur.next != null && cur.next.val == x){
cur.next = cur.next.next;
}
}else{
cur = cur.next;
}
}
return dumyHead.next;
}
}
到了这里,关于【LeetCode刷题-链表】--82.删除排序链表中的重复元素II的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!