Leetcode: Reverse Nodes in k-Group
2015-02-19 07:29
211 查看
Problem:
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:
For k = 2, you should return:
For k = 3, you should return:
Code:
/**
* 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) {
if (head == null || k == 0 || k == 1) return head;
ListNode newHead = new ListNode(0);
newHead.next = head;
ListNode prev = newHead;
ListNode current = head;
while (prev.next != null) {
int count1 = k;
ListNode tmp1 = prev;
while(tmp1.next != null && count1 >= 1) {
tmp1 = tmp1.next;
count1--;
}
if (count1 == 0) {
ListNode last = prev;
int count2 = k;
current = current.next;
ListNode p = prev.next;
prev = prev.next;
while (count2 - 1 >= 1) {
ListNode next = current.next;
current.next = prev;
// prev.next = next;
prev = current;
current = next;
count2--;
if (count2 - 1 == 0) {
last.next = prev;
prev = p;
prev.next = next;
}
}
} else {
prev = tmp1;
}
}
return newHead.next;
}
}
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
Code:
/**
* 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) {
if (head == null || k == 0 || k == 1) return head;
ListNode newHead = new ListNode(0);
newHead.next = head;
ListNode prev = newHead;
ListNode current = head;
while (prev.next != null) {
int count1 = k;
ListNode tmp1 = prev;
while(tmp1.next != null && count1 >= 1) {
tmp1 = tmp1.next;
count1--;
}
if (count1 == 0) {
ListNode last = prev;
int count2 = k;
current = current.next;
ListNode p = prev.next;
prev = prev.next;
while (count2 - 1 >= 1) {
ListNode next = current.next;
current.next = prev;
// prev.next = next;
prev = current;
current = next;
count2--;
if (count2 - 1 == 0) {
last.next = prev;
prev = p;
prev.next = next;
}
}
} else {
prev = tmp1;
}
}
return newHead.next;
}
}
相关文章推荐
- [LeetCode]Reverse Nodes in k-Group
- LeetCode Online Judge 题目C# 练习 - 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 42: Reverse Nodes in k-Group
- leetcode Reverse Nodes in k-Group
- Reverse Nodes in k-Group [LeetCode]
- 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] Swap Nodes in Pairs、Reverse Nodes in k-Group、Rotate List
- LeetCode | Reverse Nodes in k-Group
- LeetCode: Reverse Nodes in k-Group
- leetcode 42: Reverse Nodes in k-Group