• LeetCode----K 个一组翻转链表


    给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
    
    k 是一个正整数,它的值小于或等于链表的长度。
    
    如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
    
     
    
    示例:
    
    给你这个链表:1->2->3->4->5
    
    当 k = 2 时,应当返回: 2->1->4->3->5
    当 k = 3 时,应当返回: 3->2->1->4->5
    
    链接:https://leetcode-cn.com/problems/reverse-nodes-in-k-group 

    思路:比如1->2->3->4->5,k=3;

    先找到3和4的位置,然后断开,在把3前面的反转(这时候知道1和3节点)

    把4后面的递归

    把3节点和后面的节点连接起来

        /****
         * K 个一组翻转链表
         * pre k=3时
         * 1->2->3->4->5->6->7->8
         * after:
         * 3->2->1->6->5->4->7->8
         ** */
        public static ListNode reverseKGroup(ListNode head, int k) {
            ListNode temp = head;
    //       先用temp找到3的位置,不足,就返回head不反转
            for(int i=1;i<k && temp!=null;i++){
                temp=temp.next;
            }
            if (temp == null) return head;
    //        用t2记录记录4的位置,把三四断开
            ListNode  t2 = temp.next;
            temp.next=null;
    //        反转前部分,头部设置为newHead,此时head在3的位置了
            ListNode newHead = reListNode(head);
    //        把1-2-3与后面的连接起来,后面的继续递归:head.next=reverseKGroup(t2,k);
            head.next=reverseKGroup(t2,k);
            return newHead;
        }

    具体代码

    public class ListNode {
        int value;
        public ListNode(int value){
            this.value=value;
        }
        ListNode next =null;
    }
    
    
    
    
    public class TestListNode {
    
        public static void main(String[] args) {
            ListNode head = new ListNode(1);
            head.next=new ListNode(2);
            head.next.next= new ListNode(3);
            head.next.next.next=new ListNode(4);
            head.next.next.next.next=new ListNode(5);
            head.next.next.next.next.next=new ListNode(6);
            head.next.next.next.next.next.next=new ListNode(7);
            head.next.next.next.next.next.next.next=new ListNode(8);
            System.out.println("pre");
            printListNode(head);
            ListNode after = reverseKGroup(head,3);
            System.out.println("after:");
            printListNode(after);
        }
    
        /****
         * K 个一组翻转链表
         * pre k=3时
         * 1->2->3->4->5->6->7->8
         * after:
         * 3->2->1->6->5->4->7->8
         ** */
        public static ListNode reverseKGroup(ListNode head, int k) {
            ListNode temp = head;
    //       先用temp找到3的位置,不足,就返回head不反转
            for(int i=1;i<k && temp!=null;i++){
                temp=temp.next;
            }
            if (temp == null) return head;
    //        用t2记录记录4的位置,把三四断开
            ListNode  t2 = temp.next;
            temp.next=null;
    //        反转前部分,头部设置为newHead,此时head在3的位置了
            ListNode newHead = reListNode(head);
    //        把1-2-3与后面的连接起来,后面的继续递归:head.next=reverseKGroup(t2,k);
            head.next=reverseKGroup(t2,k);
            return newHead;
        }
    
        private static ListNode reListNode(ListNode head){
            ListNode next=null;
            ListNode pre=null;
            while (head!=null){
                next=head.next;
                head.next=pre;
                pre=head;
                head=next;
    
            }
            return pre;
        }
    
        private static void printListNode(ListNode node){
            ListNode temp=node;
            while (temp.next!=null){
                System.out.print(temp.value+"->");
                temp=temp.next;
    
            }
            if (temp!=null){
                System.out.println(temp.value);
            }
        }
    }
    View Code

     

  • 相关阅读:
    为同一个元素绑定多个不同的事件,并指向相同的事件处理函数方法
    js获取/设置任意元素中间内容的兼容方法
    js元素事件绑定与解绑兼容代码
    js元素事件的绑定与解绑,绑定事件的区别
    【TFS 2017 CI/CD系列
    【TFS 2017 CI/CD系列
    【TFS 2017 CI/CD系列
    【OPPO主题制作系列
    【SQL Server 学习系列】-- ConnectionTimeout、CommandTimeout和BulkCopyTimeout
    【.Net 学习系列】-- Windows身份模拟(WindowsIdentity.Impersonate)时读取Access数据库
  • 原文地址:https://www.cnblogs.com/tk55/p/13467887.html
Copyright © 2020-2023  润新知