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;
    }
相关推荐
路由侠内网穿透15 小时前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式1 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者1 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet1 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠1 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu1 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu1 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库1 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根1 天前
【CMake】List
windows·microsoft·list
TToolss1 天前
删除文件夹里的网盘图标
windows