首页 > 其他分享 > 删除排序链表中的重复元素 删除排序链表中的重复元素 II 旋转链表 字符串中第二大的数字

删除排序链表中的重复元素 删除排序链表中的重复元素 II 旋转链表 字符串中第二大的数字

时间:2022-12-03 20:00:21浏览次数:42  
标签:pre head null cur 删除 next 链表 排序

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

if(head == null) return null;
ListNode pre = head;
while(pre.next != null) {
if(pre.val == pre.next.val) {
pre.next = pre.next.next;
}else {
pre = pre.next;
}
}
return head;


82. 删除排序链表中的重复元素 II

if(head == null) return head;
ListNode pre = new ListNode(0, head);
ListNode cur = pre;
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 pre.next;


61. 旋转链表

if(head == null) return null;
if(head.next == null) return head;
ListNode l = head;
int len = 1;注意
while(l.next != null) {
len++;
l = l.next;
}
k = k % len;加速
ListNode pre = head;
for(int i = 0; i < k; i++) {将最后一个节点,放到第一位即可
ListNode root = pre;
while(root.next.next != null) {
root = root.next;
}
ListNode end = root.next;
root.next = null;
end.next = pre;
pre = end;
}
return pre;


1796. 字符串中第二大的数字

char[] pre = s.toCharArray();
int max1 = -1, max2 = -1;
for(char c : pre) {
if(Character.isLetter(c)) continue;
int num = c - '0';只有两种情况会影响max1和max2
if(num > max1) {
max2 = max1;
max1 = num;
}
else if(num > max2 && num < max1) {
max2 = num;
}
}
return max2;

标签:pre,head,null,cur,删除,next,链表,排序
From: https://www.cnblogs.com/xtag/p/16948670.html

相关文章

  • 反转链表
    92.反转链表给你单链表的头指针head和两个整数left和right,其中left<=right。请你反转从位置left到位置right的链表节点,返回反转后的链表构建一个虚拟节......
  • 输入一组长度一定的数据,从大到小(小到大)排序
    简单的排序题,课堂练习1importjava.util.Scanner;23publicclassAnimal{456publicstaticvoidmain(String[]args){8Scannerscan......
  • 静态链表
    链表的含义:将若干个结构体变量通过结构体指针联系在一起的数据结构。结点的概念:链表中的结构体变量称为链表的结点。链表的结点构成:在链表结点中纪要存储数据,也要存储下一个......
  • 算法--数组、链表、栈、队列
    一、数组1、删除有序数组中的重复项(简单)题目地址:https://leetcode.cn/problems/remove-duplicates-from-sorted-array/给你一个升序排列的数组nums,请你原地删除重......
  • MySQL Linux服务器快照克隆引起的binlog日志无法正常删除导致文件系统满
       最近,一个mysql数据库Linux服务器文件系统空间满,查看是binlog消耗绝大部分空间;经了解mysql数据库每天进行全备并删除1天前binlog日志;然而,2022.11.15日开始的binlog......
  • js向ul标签添加li并且li标签有修改删除按钮+js中添加颜色css样式(使用layui下拉)
    js向ul标签添加li并且li标签有修改删除按钮Layui当中的导航条动态添加效果:1、html放置ul标签<ulclass="layui-nav"id="nav"layui-filter="test"></ul>/2、js编......
  • Treewidget节点的删除
    父节点的删除    //第一种   //树状列表父节点的删除   //有点莽不支持这种操作deleteui->treeWidget->topLevelItem(0);    // 第二种 ......
  • SSM使用ajax实现图片上传与删除功能
    图片上传与删除​​1.上传文件​​​​2.删除数据,并且删除对应的文件​​​​3.修改上传文件至非项目路径​​之前写了一篇博客记录了关于修改资料中的图片上传​​(传送门......
  • Excel删除空格并将右侧数据左移的方法
    选择区域,进入定位后,定位到空值,最后删除空格并将右侧数据左移即可。选择区域  进入定位按F5键。 定位到空值进入定位条件后,选择空值(勾选后点击......
  • 蓝桥杯 ALGO-57算法训练 删除多余括号
    时间限制:1.0s内存限制:512.0MB问题描述从键盘输入一个含有括号的四则运算表达式,要求去掉可能含有的多余的括号,结果要保持原表达式中变量和运算符的相对位置不变,且与......