zoukankan      html  css  js  c++  java
  • 链表操作 —— 25_k个一组翻转链表

    3. 25_k个一组翻转链表
    /*
    给你这个链表:1->2->3->4->5
    
    当 k = 2 时,应当返回: 2->1->4->3->5
    
    当 k = 3 时,应当返回: 3->2->1->4->5
    */
     
    
    /*方法一*/
    class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            ListNode dummy = new ListNode(0), prev = dummy, curr = head, next;
            dummy.next = head;
            int length = 0;
            while(head != null) {
                length++;
                head = head.next;
            }
            head = dummy.next;
            for(int i = 0; i < length / k; i++) {
                for(int j = 0; j < k - 1; j++) {
                    next = curr.next;
                    curr.next = next.next;
                    next.next = prev.next;
                    prev.next = next;
                }
                prev = curr;
                curr = prev.next;
            }
            return dummy.next;
        }
    }
    
    /*方法二*/
    public class Solution {
    
        public ListNode reverseKGroup(ListNode head, int k) {
            ListNode hair = new ListNode(0);
            hair.next = head;
    
            ListNode pre = hair;
            ListNode end = hair;
    
            while (end.next != null) {
                for (int i = 0; i < k && end != null; i++){
                    end = end.next;
                }
                if (end == null){
                    break;
                }
                ListNode start = pre.next;
                ListNode next = end.next;
                end.next = null;
                pre.next = reverse(start);
                start.next = next;
                pre = start;
    
                end = pre;
            }
            return hair.next;
        }
    
        private ListNode reverse(ListNode head) {
            ListNode pre = null;
            ListNode curr = head;
            while (curr != null) {
                ListNode next = curr.next;
                curr.next = pre;
                pre = curr;
                curr = next;
            }
            return pre;
        }
    }
    
    /*方法三  递归*/
    class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            ListNode prev = null;
            ListNode cur = head;
            ListNode next = null;
            ListNode check = head;
            int canProceed = 0;
            int count = 0;
            // 检查链表长度是否满足翻转
            while (canProceed < k && check != null) {
                check = check.next;
                canProceed++;
            }
            // 满足条件,进行翻转
            if (canProceed == k) {
                while (count < k && cur != null) {
                    next = cur.next;
                    cur.next = prev;
                    prev = cur;
                    cur = next;
                    count++;
                }
                if (next != null) {
                    // head 为链表翻转后的尾节点
                    head.next = reverseKGroup(next, k);
                }
                // prev 为链表翻转后的头结点
                return prev;
            } else {
                // 不满住翻转条件,直接返回 head 即可
                return head;
            }
        }
    }
    
  • 相关阅读:
    大型网站架构系列:20本技术书籍推荐
    程序员进阶顺序
    乐观锁与悲观锁——解决并发问题
    Redis的事务功能详解
    驱动开发(8)处理设备I/O控制函数DeviceIoControl
    钱币兑换问题(杭电1284)(母函数)
    jqm文件上传,上传图片,jqm的表单操作,jqm的ajax的使用,jqm文件操作大全,文件操作demo
    问:简述一下内部类的实质是什么?
    Android 删除新版安卓fragment_main.xml
    保温饭盒毕业设计程序
  • 原文地址:https://www.cnblogs.com/s841844054/p/13736286.html
Copyright © 2011-2022 走看看