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;
    }
相关推荐
?re?ta?rd?ed?14 小时前
C++11新特性,{}与左值
c++·windows
必须得开心呀17 小时前
windows10系统任务栏图标消失修复
windows
zl_dfq18 小时前
Windows 下 UDP 套接字编程指南
网络·windows·udp
学习论之费曼学习法18 小时前
无人值守AI内容矩阵实战(10):Windows 上让 Python 进程 7×24 活着
人工智能·windows·python·矩阵·powershell·进程守护
少年的范儿19 小时前
Windows 安装 MySQL 8.4 教程(ZIP 解压版):配置 my.ini、初始化数据库与启动服务
数据库·windows·mysql
胖纸不争19 小时前
EnergyStarZ:让 Windows 后台进程给前台让路的能效管理器(v1.2 发布)
windows·性能优化·c#·win32·ef core
2501_9159090621 小时前
Windows 上用 VS Code 调试 iPhone 真机 App,KXBuild 断点、崩溃定位实操
ide·windows·vscode·ios·iphone·个人开发·敏捷流程
情 九1 天前
MCP 开发环境搭建:Mac 与 Windows 跑通 Python 服务和 stdio 客户端
windows·python·macos·mcp
懂点Tech1 天前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
云梦谭1 天前
让 Linux 主动连接 Windows:配置 Windows 11 SSH 服务端
linux·windows·ssh