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;
    }
相关推荐
航Hang*8 小时前
Windows Server 配置与管理——第3章:文件系统管理
运维·服务器·windows·vmware
无限进步_9 小时前
【C++】电话号码的字母组合:从有限处理到通用解法
开发语言·c++·ide·windows·git·github·visual studio
私人珍藏库10 小时前
【Windows】PDF超能助手(1.0.13)
windows·pdf·工具·软件·多功能
仟人斩10 小时前
Windows 下把 VSCode 加入右键菜单(注册表方案)
windows·vscode·上下文菜单
大强同学10 小时前
对比 VS Code:Zed 编辑器编码体验全面解析
人工智能·windows·编辑器·ai编程
加勒比之杰克14 小时前
从阻塞 IO 到 epoll:把 Linux 网络 IO 一次讲透
linux·网络·windows·select·多路转接·epoll·poll
涔溪15 小时前
腾讯 WorkBuddy 超详细卸载清理文档(适用于 Windows 1011 + macOS 全版本,彻底卸载、不留残留)
windows·macos·ai·workbuddy
取个名字太难了a15 小时前
等待块(一)
windows
洒满阳光的庄园15 小时前
Tauri Windows 桌面端:环境与构建流程
windows
love530love15 小时前
从零搭建本地版 Claurst:基于 Rust 重构的 Claude Code 终端编码助手 + LM Studio 模型接入测试
开发语言·人工智能·windows·重构·rust·lm studio·claude code