当前位置:网站首页>Leetcode82. 删除排序链表中的重复元素 II
Leetcode82. 删除排序链表中的重复元素 II
2022-08-09 23:32:00 【Java全栈研发大联盟】
题目传送地址:https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/
运行效率
代码如下:
//递归解法
class Solution {
public static ListNode deleteDuplicates(ListNode head) {
//处理边界情况
if (head == null || head.next == null) {
return head;
}
int nextVal = head.next.val;
ListNode listNode = deleteDuplicates(head.next);
//处理特殊情况
if(listNode==null){
//比如head=[3,3,3]这种情况
if(nextVal== head.val){
return null;
}
//比如head={1,2,2}
head.next=null;
return head;
}
//比如这种情况[1,1,1,2,3] head.val=1 head.next=[1,1,2,3] , 这个时候返回的listNode=[2,3]
if (head.val == nextVal && listNode.val != nextVal) {
return listNode;
}
if (head.val == listNode.val) {
return listNode.next;
} else {
head.next = listNode;
return head;
}
}
}
边栏推荐
猜你喜欢
781. 森林中的兔子
Creo5.0入门教程赠素材
MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]
源码编译安装LAMP和LNMP
[SUCTF 2019]CheckIn (.htaccess和.user.ini)
【集训DAY4】矩形【线段树】
【SSL集训DAY2】Sequence【数学】
【SSL集训DAY3】控制棋盘【二分图匹配】
Digital wallets, red sea ecological rapid introduction of small programs can help capture device entry wisdom
Golden Warehouse Database KingbaseGIS User Manual (6.5. Geometry Object Editing Function)
随机推荐
FreeRTOS任务基础
LeetCode常见题型——链表
NTU General Database-Gbase-8a-Learning-04-Deploying Distributed Clusters
线程的同步与互斥
Redis 大 key 要如何处理?
[Cloud native] Kubernetes orchestration tools
const修饰指针的三种情况
CAD 绘制圆角处理
deepstream学习笔记(三):deepstream-imagedata-multistream解析与接入适配yolov5模型测试
考柏的感慨
Jpa 查询view or 无主键的table
Project (7) - PolarSeg point cloud semantic segmentation
Leecode-205. 同构字符串
【剑指offer】第一题 第二题
arm-4-裸板开发
【集训DAY3】挖金矿【二分答案】
Today's sleep quality record 61 points
AirFlow介绍
共创 Ray 中文社区,Ray Forward Meetup 2022 直播邀你参加!
Why don't suggest you run in Docker Mysql?