Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
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
Note:
Only constant extra memory is allowed.
You may not alter the values in the list’s nodes, only nodes itself may be changed.
/**
* 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==NULL||k==1)return head;
int cnt=0;
ListNode* p=new ListNode(-1);
p->next=head;
ListNode* cur=p,*nxt;
ListNode* pre=p;
while(cur=cur->next)cnt++;
while(cnt>=k){
cur=pre->next;
nxt=cur->next;
for(int i=1;i<k;i++){
cur->next=nxt->next;
nxt->next=pre->next;
pre->next=nxt;
nxt=cur->next;
}
pre=cur;
cnt-=k;
}
return p->next;
}
};
迭代
Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode p;
if(head==null||head.next==null||k==1)return head;
ListNode dummy=new ListNode(-1);
dummy.next=head;
p=dummy;
int i=0;
while(head!=null){
i++;
if(i%k==0){
p=reverse(p,head.next);
head=p.next;
}else head=head.next;
}
return dummy.next;
}
public ListNode reverse(ListNode s,ListNode e){
ListNode cur=s.next;
ListNode nxt,p;
ListNode pre=s;
p=cur;
while(cur!=e){
nxt=cur.next;
cur.next=pre;
pre=cur;
cur=nxt;
}
s.next=pre;
p.next=cur;
return p;
}
}
- 另一种写法:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode p;
if(head==null||head.next==null||k==1)return head;
ListNode dummy=new ListNode(-1);
dummy.next=head;
p=dummy;
int i=0;
while(head!=null){
i++;
if(i%k==0){
p=reverse(p,head.next);
head=p.next;
}else head=head.next;
}
return dummy.next;
}
public ListNode reverse(ListNode s,ListNode e){
ListNode cur=s.next,nxt;
while(cur.next!=e){
nxt=cur.next;
cur.next=nxt.next;
nxt.next=s.next;
s.next=nxt;
}
return cur;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head==null||head.next==null||k==1)return head;
ListNode dummy=new ListNode(-1);
dummy.next=head;
ListNode pre=dummy,cur,nxt,end;
int i=0;
while(head!=null){
i++;
if(i%k==0){
cur=pre.next;
end=head.next;
while(cur.next!=end){
nxt=cur.next;
cur.next=nxt.next;
nxt.next=pre.next;
pre.next=nxt;
}
pre=cur;
head=pre.next;
}else head=head.next;
}
return dummy.next;
}
}
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==NULL||head->next==NULL||k==1)return head;
ListNode* dummy=new ListNode(-1);
dummy->next=head;
ListNode* pre=dummy,* cur,* nxt,* end;
int i=0;
while(head!=NULL){
i++;
if(i%k==0){
end=head->next;
cur=pre->next;
while(cur->next!=end){
nxt=cur->next;
cur->next=nxt->next;
nxt->next=pre->next;
pre->next=nxt;
}
pre=cur;
head=cur->next;
}else head=head->next;
}
return dummy->next;
}
};
Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
if head is None or head.next is None or k==1:
return head
dummy=ListNode(-1, head)
pre=dummy
cnt=0
while head is not None:
cnt+=1
if cnt%k==0:
cur=pre.next
end=head.next
while cur.next!=end:
nxt=cur.next
cur.next=nxt.next
nxt.next=pre.next
pre.next=nxt
pre=cur
head=cur.next
else:
head=head.next
return dummy.next
递归
Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head==null||head.next==null||k==1)return head;
ListNode cur=head;
int cnt=0;
while(cur!=null&&cnt!=k){
cur=cur.next;
cnt++;
}
if(cnt!=k)return head;
ListNode p=reverseKGroup(cur, k);
for(int i=0;i<k;i++){
ListNode nxt=head.next;
head.next=p;
p=head;
head=nxt;
}
head=p;
return head;
}
}
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==NULL||head->next==NULL||k==1)return head;
ListNode* cur=head,* nxt;
int cnt=0;
while(cur!=NULL&&cnt!=k){
cur=cur->next;
cnt++;
}
if(cnt!=k)return head;
ListNode* p=reverseKGroup(cur, k);
cur=head;
ListNode* dummy=new ListNode(-1);
dummy->next=head;
ListNode* pre=dummy;
for(int i=1;i<k;i++){
nxt=cur->next;
cur->next=nxt->next;
nxt->next=pre->next;
pre->next=nxt;
}
cur->next=p;
return dummy->next;
}
};
- 不用
dummy
的写法
/**
* 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==NULL||head->next==NULL||k==1)return head;
ListNode* cur=head,* nxt;
int cnt=0;
while(cur!=NULL&&cnt!=k){
cur=cur->next;
cnt++;
}
if(cnt!=k)return head;
ListNode* p=reverseKGroup(cur, k);
for(int i=1;i<k;i++){
nxt=head->next;
head->next=p;
p=head;
head=nxt;
}
nxt->next=p;
return head;
}
};
Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
if head is None or head.next is None or k==1:
return head
cur=head
cnt=0
while cur!=None and cnt!=k:
cur=cur.next
cnt+=1
if cnt!=k:
return head
p=self.reverseKGroup(cur,k)
for _ in range(k):
nxt=head.next
head.next=p
p=head
head=nxt
head=p
return head