LeetCode-Reverse Nodes in k-Group
来源:互联网 发布:淘宝宝贝曝光量怎么查 编辑:程序博客网 时间:2024/06/11 01:12
直接数了个数 算了要做几个reverse 然后直接做的
可以优化的就是不需要数有多少个 每次累加到够了k就做一次 其实差不多
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reverseKGroup(ListNode head, int k) { if ( head == null || k <= 1 ) return head; int length = 0; ListNode pt = head; while ( pt != null ){ pt = pt.next; length ++; } int times = length / k; ListNode dummy = new ListNode ( 0 ); dummy.next = head; pt = dummy; for ( int i = 0; i < times; i ++ ){ ListNode pre = head, cur = head.next; for ( int j = 1; j < k; j ++ ){ ListNode next = cur.next; cur.next = pre; pre = cur; cur = next; } pt.next.next = cur; pt.next = pre; pt = head; head = head.next; } return dummy.next; }}
0 0
- LeetCode:Reverse Nodes in k-Group
- LeetCode: Reverse Nodes in k-Group
- LeetCode : Reverse Nodes in k-Group
- leetcode 42: Reverse Nodes in k-Group
- [Leetcode] reverse nodes in K-Group
- [LeetCode]Reverse Nodes in k-Group
- LeetCode-Reverse Nodes in k-Group
- LeetCode - Reverse Nodes in k-Group
- LeetCode | Reverse Nodes in k-Group
- 【leetcode】Reverse Nodes in k-Group
- Leetcode: Reverse Nodes in k-Group
- leetcode Reverse Nodes in k-Group
- [LeetCode] Reverse Nodes in k-Group
- LeetCode题解: Reverse Nodes in k-Group
- 【Leetcode】Reverse Nodes in k-Group
- LeetCode Reverse Nodes in k-Group
- [Leetcode] Reverse Nodes in k-Group (Java)
- LeetCode(25)Reverse Nodes in K-Group
- window下添加tomcat为服务启动启动,且待oracle服务启动后再启动
- 黑马程序员——OC语言加强---ARC使用特点及注意事项
- background-position
- leetcode 64.Ugly Number-丑数
- ORACLE数据库间快速导入数据的方法
- LeetCode-Reverse Nodes in k-Group
- c++ vector各种函数介绍及其用法
- 欧拉回路(poj1041)
- codeforces 494B B. Obsessive String(dp)
- 深入Java集合学习系列:TreeSet 详解
- 结构体中的位域及小端问题
- StringBuffer和StringBuilder
- 快次幂
- HDU1026 Ignatius and the Princess IV(java)