【Leetcode】之Reverse Nodes in k-Group

原创 2015年11月18日 10:28:44

一.问题描述

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

二.我的解题思路

链表的问题都很直观。对于这道题目,很自然的想到分治递归的办法。以k个节点为一个单位,进行reverse求解。测试通过的程序如下:
/**
 * 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) {
            ListNode* curr,* next,* tmp;
            ListNode* st,* end;
            int cnt=0;
            curr=head;
            if(curr==NULL) return NULL; 
            while(curr!=NULL){
                cnt++;
                if(cnt==k)
                    break;
                curr=curr->next;
            }   //judge if there exists k nodes
            if(cnt==k){ //if exists
               st=head;cnt=1;end=head;
                while(cnt<k){
                    next=end->next;
                    tmp=next->next;
                    next->next=st;
                    end->next=tmp;
                    st=next;
                    cnt++;
                }
                if(end->next==NULL)
                    return st;
                end->next=reverseKGroup(end->next,k);
                return st;
            }
            
            return head;
                
            
            
            
            
            
    }
};



相关文章推荐

leetcode025:Reverse Nodes in k-Group

问题描述 Given a linked list, reverse the nodes of a linked list k at a time and return its modified l...

leetcode之Partition List,Reverse Nodes in k-Group

Partition List Given a linked list and a value x, partition it such that all nodes less than x com...

leetcode25---Reverse Nodes in k-Group

问题描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified l...
  • will130
  • will130
  • 2015年12月20日 23:59
  • 273

LeetCode (25) Reverse Nodes in k-Group (链表)

题目描述Given a linked list, reverse the nodes of a linked list k at a time and return its modified list...

LeetCode 25. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k...

[Leetcode] Reverse Nodes in k-Group (Java)

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. I...

[LeetCode]25.Reverse Nodes in k-Group

【题目】 Given a linked list, reverse the nodes of a linked list k at a time and return its modified l...

[LeetCode][Java] Reverse Nodes in k-Group

题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified...

Leetcode25 Reverse Nodes in k-Group

Reverse Nodes in k-Group My Submissions Question Solution Given a linked list, reverse the nod...

[leetcode] 25. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. I...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Leetcode】之Reverse Nodes in k-Group
举报原因:
原因补充:

(最多只允许输入30个字)