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;
            }
        }
    }
    
  • 相关阅读:
    企业使用数据库的12种姿势
    回归架构本质,重新理解微服务
    Java中随机数的产生方式与原理
    自动类型转换、强制类型转换、作用域、整型表数范围
    创建自定义类的对象数组
    CentOS上安装比较习惯的代码编辑器
    ubuntu 15.04 的安装遇到的问题及其解决方法
    算法思想篇(1)————枚举算法
    初来乍到
    Eclipse中获取html jsp 标签的属性提示信息方法
  • 原文地址:https://www.cnblogs.com/s841844054/p/13736286.html
Copyright © 2011-2022 走看看