Algorithms—25.Reverse Nodes in k-Group
来源:互联网 发布:mac os x 10.12懒人版 编辑:程序博客网 时间:2024/05/17 02:06
思路:把每段需要交换的数值读出来,然后再按要求赋值回去。
/** * 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) {ListNode l = head;List<Integer> val = new ArrayList<Integer>();for (int i = 0; i < k; i++) {if (l == null) {return head;}val.add(l.val);l = l.next;}l = head;for (int i = val.size() - 1; i >= 0; i--) {l.val = val.get(i);l = l.next;}l = reverseKGroup(l, k);return head;}}/** * 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) {ListNode l = head;List<Integer> val = new ArrayList<Integer>();for (int i = 0; i < k; i++) {if (l == null) {return head;}val.add(l.val);l = l.next;}l = head;for (int i = val.size() - 1; i >= 0; i--) {l.val = val.get(i);l = l.next;}l = reverseKGroup(l, k);return head;}}
耗时:388ms,下游,最好的测试过316ms,时间不稳定。
0 0
- Algorithms—25.Reverse Nodes in k-Group
- LeetCode-algorithms 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
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 【LeetCode】25.Reverse Nodes in k-Group
- [leetcode] 25.Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- Leetcode-25.Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- leetcode 25. Reverse Nodes in k-Group
- 微信登录的文章-二维码网页登录和微信中登录
- 手势简单应用
- 云计算、HANA支撑业务双位数增长
- Codeforces Round #313 (Div. 2) A B C D
- u Calculate e
- Algorithms—25.Reverse Nodes in k-Group
- php字符串
- MyBatis学习系列二:单表查询一
- ShareSdk无法修改Left Right两个Button的颜色问题
- zookeeper会话超时
- .net Event/Delegate Simple Demo
- Myeclipse导出jar文件图片路径问题
- 大数乘法
- 【svn】Eclipse里删了的文件,svn更新不回来