d202541

目录

一、分隔链表

二、旋转链表

三、删除链表中重复的数字


一、分隔链表

用两个list存一下小于和大于等于 x的节点

最后串起来就行

java 复制代码
 public ListNode partition(ListNode head, int x) {
        ListNode ret = new ListNode(1);
        ListNode cur = ret;
        List<ListNode> small = new ArrayList<>();
        List<ListNode> big = new ArrayList<>();
        List<ListNode> equal = new ArrayList<>();
        while(head != null){
            if(head.val < x){
                small.add(head);
            } else if(head.val >= x){
                big.add(head);
            }
            head = head.next;
        }
        for(int i = 0;i < small.size();i++){
            cur.next = small.get(i);
            cur = cur.next;
        }
        for(int i = 0;i < big.size();i++){
            cur.next = big.get(i);
            cur = cur.next;
        }
        if(big.size() > 0){
            big.get(big.size() - 1).next = null;
        } else if(small.size() > 0){
            small.get(small.size() - 1).next = null;
        }
        return ret.next;    
    }

二、旋转链表

题目意思就是每次把末尾最后一个放到最左边

可以用list集合存一下 每次去掉最后一个,并把对其头插

需要考虑一下k特别大的情况 取余一下

java 复制代码
 public ListNode rotateRight(ListNode head, int k) {
        List<ListNode> list = new LinkedList<>();
        ListNode ret = new ListNode(1);
        ListNode cur = ret;
        while(head != null){
            list.add(head);
            head = head.next;
        }
        if(list.size() > 0){
            k = k % list.size();
        }
        while(k-- != 0){
            if(list.size() > 0){
                 ListNode first = list.get(list.size() - 1);
                list.remove(list.size() - 1);
                list.addFirst(first);
            }
        }
        for(int i = 0;i < list.size();i++){
            cur.next = list.get(i);
            cur = cur.next;
        }
        cur.next = null;
        return ret.next;
    }

三、删除链表中重复的数字

用map记录一下,哪些是重复数字,哪些不是重复数字

java 复制代码
public ListNode deleteDuplicates(ListNode head) {
        Map<Integer,Boolean> map = new HashMap<>();
        ListNode cur = head;
        while(cur != null){
            if(map.containsKey(cur.val)){
                map.put(cur.val,false);
            }else{
                map.put(cur.val,true);
            }
            cur = cur.next;
        }
        cur = head;
        ListNode ret = new ListNode(1);
        ListNode curRet = ret;
        while(cur != null){
            if(map.get(cur.val)){
               curRet.next = cur;
               curRet = curRet.next;
            }
            cur = cur.next;
        }
        curRet.next = null;
        return ret.next;
    }
相关推荐
阿白的白日梦2 天前
winget基础管理---更新/修改源为国内源
windows
埃博拉酱6 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21887 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号37 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest7 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅7 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技7 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~7 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
一个假的前端男7 天前
[特殊字符] Flutter 安装完整指南 Windows—— 2026最新版
windows·flutter
倚肆7 天前
在 Windows Docker 中安装并配置 Nginx (映射 Windows 端口与路径)
windows·nginx·docker