leetcode 42: Reverse Nodes in k-Group
来源:互联网 发布:微信一键加附近人软件 编辑:程序博客网 时间:2024/05/06 01:25
Feb 16 '12
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { // Start typing your C/C++ solution below // DO NOT write int main() function if( !head || k<=1) return head; ListNode ** p = &head; ListNode * cur = head; ListNode * pre = NULL; ListNode * end; while(true) { int i=k; // probe first ListNode * temp = cur; while( temp!=NULL && --i>0) { temp = temp->next; } if(!temp) return head; i=k; while(i-->0) { end = cur->next; cur->next = pre; pre = cur; cur = end; } (*p)->next = cur; ListNode * t = *p; *p = pre; p = &(t->next); pre = NULL; } return head; }};
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode reverseKGroup(ListNode head, int k) { // Start typing your Java solution below // DO NOT write main() function if(head==null || k==1) return head; ListNode start = new ListNode(0); start.next = head; ListNode pre = null; ListNode cur = head; ListNode next = null; ListNode end = null; head = start; while( cur != null) { int i=k; end = cur; while(--i>0 && end != null ){ end = end.next; } if(end==null) return head.next; i=k; while(i-->0) { next = cur.next; cur.next = pre; pre = cur; cur = next; } start.next.next = cur; ListNode t = start.next; start.next = pre; start = t; } return head.next; }}
- 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
- 【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
- Leetcode: Two Sum
- 2012年前25个面试怪题
- 一个纠结的难题的java实现
- 第一篇 - 敏捷学习笔记
- Leetcode: atoi
- leetcode 42: Reverse Nodes in k-Group
- 面试笔记3
- OGNL在Struts2中的应用
- java集合类详解
- #读书笔记#松本行弘的程序设计·第一章
- 纠结的List remove
- 博客简介
- 17(39)---windows internal 第6版 mark david alex
- 基于Eht 开发框架 DataGrid 列分组,翻页,添加代码、列拖拽及DataPickter的实现