<习题集><LeetCode><链表><61/83/82/86/92>


61. 旋转链表

https://leetcode.cn/problems/rotate-list/

    public ListNode rotateRight(ListNode head, int k) {
        //k等于0,或者head为空,直接返回head;
        if(k == 0 || head == null){
            return head;
        }
        //创建last用于记录尾节点,移动last找到尾节点,同时计算链表长度n;
        ListNode last = head;
        int n = 1;
        while(last.next != null){
            n++;
            last = last.next;
        }
        //k取模,避免重复翻转的情况;
        k = k%n;
        //k等于0,不用翻转直接返回head;
        if(k == 0){
            return head;
        }
        //创建移动的指针节点,找到开始翻转的节点的前一个节点;
        ListNode cur = head;
        int step = n-k;
        for(int i=step-1;i>0;i--){
            cur = cur.next;
        }
        //记录开始翻转的节点,这个节点就是返回的新链表的头节点。
        ListNode root = cur.next;
        //将尾节点和头节点连接;
        last.next = head;
        //截断开始翻转的前一个节点;
        cur.next = null;

        return root;
    }

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

https://leetcode.cn/problems/remove-duplicates-from-sorted-list/

    public ListNode deleteDuplicates(ListNode head) {
        //head为空,直接返回head;
        if (head == null) {
            return head;
        }
        //创建一个用于移动的指针节点;
        ListNode cur = head;
        //遍历链表,下一节点为空时,代表遍历完成;
        while (cur.next != null) {
            //如果前后节点值相等,则将后节点删除,前节点指向更后一个节点;
            if(cur.val == cur.next.val){
                cur.next = cur.next.next;
            }else{
                //否则向后移动;
                cur = cur.next;
            }
        }

        return head;
    }

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

https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/

    public ListNode deleteDuplicates(ListNode head) {
        //如果链表为空,返回null;
        if(head == null){
            return null;
        }

        //创建一个用于移动的指针节点,这个节点初始位置在head前;
        ListNode cur = new ListNode(-1,head);
        //记录节点cur的初始位置,用于返回;
        ListNode root = cur;

        //当cur的后两个位置有值时;
        while(cur.next!=null && cur.next.next!=null){
            //判断两个值是否相等;
            if(cur.next.val == cur.next.next.val){
                //相等则记录前一个值;
                ListNode curNext = cur.next;
                //在后一个值存在的前提下,将前一个值不断和后一个值比较;
                while(curNext.next!=null && curNext.val == curNext.next.val){
                    //如果前后值相等,则前值后移;
                    curNext = curNext.next;
                }
                //通过上述循环,走到此处则是遇到前后值不同的两个节点了;
                //将移动的指针节点cur的next,指向这个不同的后值。
                cur.next = curNext.next;
            }else{
                //如果前后值不相等,则cur向后移动;
                cur = cur.next;
            }
        }
        //返回记录的节点;
        return root.next;
    }

86. 分隔链表

https://leetcode.cn/problems/partition-list/

    public ListNode partition(ListNode head, int x) {
        //如果链表为空,直接返回;
        if(head == null){
            return null;
        }
        //建立两个链表,分别存储小于x和大于等于x的节点;
        ListNode less = new ListNode(-1);
        ListNode lCur = less;
        ListNode greater = new ListNode(-1);
        ListNode gCur = greater;
        //移动head节点,根据head节点值的大小为上面两个链表分配节点;
        while(head != null){
            if(head.val < x){
                lCur.next = head;
                lCur = lCur.next;
            }else{
                gCur.next = head;
                gCur = gCur.next;
            }
            head = head.next;
        }
        //greater链表肯定是放在两个链表偏后的,将这个链表的尾部置空,避免环形链表;
        gCur.next = null;
        //如果less为空,直接返回greater链表,注意返回的是next;
        if(less.next == null){
            return greater.next;
        }else{
            //如果less不为空,将less链表的尾部指向greater链表后,返回;
            lCur.next = greater.next;
            return less.next;
        }
    }

92. 反转链表 II

https://leetcode.cn/problems/reverse-linked-list-ii/

    public ListNode reverseBetween(ListNode head, int left, int right) {
        //因为head有可能被改变,创建一个节点,指向head,用于返回;
        ListNode root = new ListNode(-1,head);
        //创建节点pre,并将pre移动到指定开始翻转的节点的前一个节点;
        ListNode pre = root;
        for (int i=0;i<left-1;i++) {
            pre = pre.next;
        }
        //创建一个节点cur,cur指向需要反转的第一个节点;
        //cur最终会转到需要反转的最后一个节点位置上;
        ListNode cur = pre.next;

        for(int i=0;i<right-left;i++){
            //先记录cur的下一个节点;
            ListNode curNext = cur.next;
            //将cur的下一节点指向下下个节点;
            cur.next = curNext.next;
            //将curNext的下一节点指向pre的下一节点;
            //注意不要指向cur,因为cur一直在移动;
            curNext.next = pre.next;
            //将pre的下一节点指向curNext;
            //因为每次curNext都会更新;
            //这代表pre的下一节点指向的节点会随着循环的进行逐渐指向right的那个节点;
            pre.next =curNext;
        }
        return root.next;
    }

相关推荐

  1. LeetCode——

    2023-12-08 17:34:05       56 阅读
  2. leetcode-环形

    2023-12-08 17:34:05       63 阅读
  3. leetcode-相交

    2023-12-08 17:34:05       57 阅读
  4. leetcode-专题

    2023-12-08 17:34:05       54 阅读
  5. leetcode-

    2023-12-08 17:34:05       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 17:34:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 17:34:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 17:34:05       82 阅读
  4. Python语言-面向对象

    2023-12-08 17:34:05       91 阅读

热门阅读

  1. Fiddler抓包测试

    2023-12-08 17:34:05       60 阅读
  2. Vue+ElementUI实现输入框日期框下拉框动态展示

    2023-12-08 17:34:05       60 阅读
  3. 常用的git版本控制有哪些工具或网站呢?

    2023-12-08 17:34:05       73 阅读
  4. Git 还原文件修改

    2023-12-08 17:34:05       59 阅读
  5. 求int型正整数在内存中存储时1的个数

    2023-12-08 17:34:05       49 阅读
  6. 程序员学习方法

    2023-12-08 17:34:05       57 阅读
  7. flask之文件上传

    2023-12-08 17:34:05       63 阅读
  8. JDK、JRE、JVM、SE、EE、ME的区别

    2023-12-08 17:34:05       46 阅读
  9. Requests库详解、详细使用、高级用法

    2023-12-08 17:34:05       51 阅读
  10. 折半查找(数据结构实训)

    2023-12-08 17:34:05       65 阅读