LeetCode 第25题 Reverse Nodes in k-Group
来源:互联网 发布:麦肯锡数据分析报告 编辑:程序博客网 时间:2024/06/06 15:01
一 ,问题描述
1,题目的意思是将链表进行部分逆置,逆置的部分跟K值有关。
2,如上所示,
对于 1->2->3->4->5
当k=1时,输出:1->2->3->4->5
当k=2时 , 输出: 2->1->4->3->5
当k=3时 ,输出: 3->2->1->4->5 其中 后面 4 5没变是因为做完一次 3 2 1 剩下的 4 5 已经比k小了,所以保持原来的顺序
3,解题思路;
首先分析链表的长度是否为k的倍数,
如果是的话,则 先每次找到k个节点,然后把这个k个节点放入新建的数组中,数组长度为k,把k个节点里的值插入到反着的数组中。然后在把正的数组的值复制到这k个节点。这样就完成了k个节点的反转。
如果不是的话,则先找到k个节点,如果有k个节点的话,则然后把这个k个节点放入新建的数组中,数组长度为k,把k个节点里的值插入到反着的数组中。然后在把正的数组的值复制到这k个节点。这样就完成了k个节点的反转;如果没有k个节点的话,那意味着到了末尾几个节点了,所以不处理它们。
二,AC了的程序(JAVA实现的)
import java.util.Scanner;class ListNode{ int val; ListNode next; ListNode(int x) { val=x; }}public class Test2{ public static ListNode reverseKGroup(ListNode head, int k) { ListNode p1=head; ListNode p2=head; ListNode t; ListNode t2; ListNode t3; int len=0; if(head==null) { return null; } while(p1!=null) { len++; p1=p1.next; } if(len<k) { return head; } if(k==0||k==1) { return head; } if(len%k==0) { while(p2!=null) { t2=p2; t3=p2; int []array=new int[k]; for(int i=0;i<k-1;i++) { if(p2.next!=null) { p2=p2.next; } } for(int i=k-1;i>=0;i--) { if(t2!=p2.next) { array[i] = t2.val; t2=t2.next; } } for(int i=0;i<array.length;i++) { t3.val=array[i]; t3=t3.next; } p2=p2.next; } } else { int n; while(p2!=null) { n=1; t2=p2; t3=p2; for(int i=0;i<k-1;i++) { if(p2.next!=null) { p2=p2.next; } else { n=0; break; } } if(n!=0) { int []array=new int[k]; for(int i=k-1;i>=0;i--) { if(t2!=p2.next) { array[i] = t2.val; t2=t2.next; } } for(int i=0;i<array.length;i++) { t3.val=array[i]; t3=t3.next; } p2=p2.next; } else { break; } } } return head; } public static void main(String []args) { Scanner scan=new Scanner(System.in); ListNode head=new ListNode(1); ListNode p1=new ListNode(2); ListNode p2=new ListNode(3); ListNode p3=new ListNode(4); ListNode p4=new ListNode(5); ListNode p5=new ListNode(6); ListNode p6=new ListNode(7); ListNode p7=new ListNode(8); /*ListNode p8=new ListNode(9); */ int k; k=scan.nextInt(); head.next=p1; p1.next=p2; p2.next=p3; p3.next=p4; p4.next=p5; p5.next=p6; p6.next=p7; /* p7.next=p8; */ ListNode newNode=reverseKGroup(head,k); while(newNode!=null) { System.out.print(newNode.val+" "); newNode=newNode.next; } }}
运行结果图:
1 0
- LeetCode第25题之Reverse Nodes in k-Group
- LeetCode 第25题 Reverse Nodes in k-Group
- LeetCode(25)Reverse Nodes in K-Group
- [leetcode 25]Reverse Nodes in k-Group
- [leetcode #25]Reverse Nodes in k-Group
- leetcode 25 Reverse Nodes in k-Group
- Leetcode 25 Reverse Nodes in k-Group
- LeetCode 25 - Reverse Nodes in k-Group
- LeetCode 25: Reverse Nodes in k-Group
- leetcode 25 -- Reverse Nodes in k-Group
- leetcode 25:Reverse Nodes in k-Group
- leetcode 25: Reverse Nodes in k-Group
- [Leetcode 25] Reverse Nodes in k-Group
- Leetcode#25||Reverse Nodes in k-Group
- LeetCode 25 Reverse Nodes in k-Group
- leetcode-25 Reverse Nodes in k-Group
- LeetCode 25: Reverse Nodes in k-Group
- Leetcode #25 Reverse Nodes in k-Group
- $.each函数
- Fedora下安装Sun JDK并设置默认JDK
- android studio 使用错误
- NMap和ZMap
- thinkphp 5.0多用户商城 又添新产品
- LeetCode 第25题 Reverse Nodes in k-Group
- C控制台实现模拟平抛运动算法
- Java并发编程:线程池的使用
- Android 进程保活招式大全
- 【递归】 之 逆波兰表达式
- angularjs请求服务器报405,数据格式不对,以x-www-form-urlencoded传输
- 什么是操作系统
- elasticsearch配置文件详解
- 【编程工具】Pycharm 开启远程SSH Terminal