题目:
Given a linked list, reverse the nodes of a linked listk 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
题解:
先放上自己又长又ugly的解法作为反面教材,虽然算法正确,但是因为运行时间过长无法通过大集合。
思路是用三个指针进行反转链表操作。
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
newHead = head;
nextHead = head;
nextNewHead = head;
if(!head || k <= 1)
return head;
int length = 0;
ListNode *p = head;
while(p)
{
p = p->next;
length++;
}
int round = length / k;
while(round)
{
if(round > 1)
{
int i = 2*k;
ListNode * temp = nextHead;
while( i > 1 )
{
temp = temp->next;
i--;
}
nextNewHead = temp;
}
else
{
int i = k;
ListNode * temp = nextHead;
while( i > 0 )
{
temp = temp->next;
i--;
}
nextNewHead = temp;
}
ListNode *f = nextHead;
ListNode *s = f->next;
ListNode *t = s->next;
int count = 1;
while(count < k)
{
s->next = f;
if(count == 1)
f->next = nextNewHead;
f = s;
s = t;
if( t && t->next)
t = t->next;
else
t = NULL;
count++;
}
nextHead = s;
if(round == length / k)
newHead = f;
round--;
}
ListNode *x = newHead;
while(x)
{
cout << x->val << endl;
x = x->next;
}
return newHead;
}
ListNode *newHead = NULL;
ListNode *nextHead = NULL;
ListNode *nextNewHead = NULL;
};
然后学习一下牛人的解法。
首先是插入法实现链表反转, 需要创建dummy头结点:
ListNode *reverseAll(ListNode* head)
{
ListNode* dummy = new ListNode(0);
ListNode* last = head;
ListNode* cur = last->next;
dummy->next = last;
while(cur)
{
last->next = cur->next;
cur->next = dummy->next;
dummy->next = cur;
cur = last->next;
}
head = dummy->next;
ListNode* p = head;
while(p)
{
cout << p->val << " -> ";
p = p->next;
}
return head;
}
然后是链表中任意两节点之间的节点反转(不包括两端的节点):
ListNode * reversePart(ListNode * pre, ListNode * next)
{
ListNode* last = pre->next;
ListNode* cur = last->next;
while(cur != next)
{
last->next = cur->next;
cur->next = pre->next;
pre->next = cur;
cur = last->next;
}
return last;
}
注意上面方法的返回值时当前反转序列反转后的最后一个节点的位置,也就是下一次反转的pre的值,用计数器统计遍历节点的个数,每当遍历到k的倍数时,调用上面函数进行局部反转。
ListNode *reverseKGroup(ListNode *head, int k) {
if(!head || k <= 1)
return head;
ListNode* dummy = new ListNode(0);
ListNode* pre = dummy;
dummy->next = head;
int count = 0;
while(head)
{
count++;
if(!(count % k))
{
pre = reversePart(pre, head->next);
head = pre->next;
}
else
head = head->next;
}
ListNode* p = dummy->next;
while(p)
{
cout << p->val << " -> ";
p = p->next;
}
return dummy->next;
}
7个月后重做, 还是费了些脑筋,不过已经可以自己想出来了。还是递归,每次返回头指针。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @param k, an integer
# @return a ListNode
def reverseKGroup(self, head, k):
length = 0
p = head
while p != None:
length += 1
p = p.next
it = length / k
if iter == 0:
return head
else:
head = self.reverse(head, it, k)
return head
def reverse(self, head, it, k):
if it == 0:
return head
head_new = head
pre = head_new
cur = pre.next
for i in range(1, k):
temp = cur.next
cur.next = pre
pre = cur
cur = temp
head_new.next = self.reverse(cur, it-1, k)
return pre
Java版:
/**
* 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) {
ListNode p = head;
int length = 0;
while( p != null ) {
length += 1;
p = p.next;
}
int iter = length / k;
if(iter == 0)
return head;
head = reverse(head, iter, k);
return head;
}
private ListNode reverse(ListNode head, int iter, int k) {
if(iter == 0)
return head;
ListNode headNew = head;
ListNode pre = headNew;
ListNode cur = pre.next;
for(int i = 1; i < k; i++) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
headNew.next = reverse(cur, iter-1, k);
return pre;
}
}
C++版:
/**
* 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) {
if(!head || !k) return head;
ListNode* p = head;
int length = 0;
while(p) {
length++;
p = p->next;
}
int iter = length / k;
if(!iter) return head;
head = reverse(head, iter, k);
return head;
}
ListNode* reverse(ListNode* head, int iter, int k) {
if(!iter) return head;
ListNode* pre = head;
ListNode* cur = pre->next;
ListNode* temp;
for(int i = 1; i < k; i++) {
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
head->next = reverse(cur, iter-1, k);
return pre;
}
};