leetcode-25-Reverse Nodes in k-Group

原创 2016年05月31日 22:24:04

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个元素倒序。

插播:链表倒叙函数。

void List::reverse()
{
    Node * p = head->next;//头结点之后的第1个节点
    Node * q = head->next->next;//头结点之后的第2节点
    Node * m = head->next->next->next;//头结点之后的第3个节点
    p->next = NULL;//将头接点之后的第1个节点的next指针置为空
    //根据m是否为空来判断 以此逆序每一个节点
    while(m){
        q->next = p;
        p = q;
        q = m;
        m = m->next;
    }
    //将最后一个节点逆序
    q->next = p;
    //将头从新指向新的的第1个节点(之前的最后一个节点)
    head ->next = q;
}

在这道题里面,要将倒序函数修改。有参数,即这k个元素的起始元素。
重要的一点是一部分链表改变之后如何和其余的链表整合。一个方法是函数返回值。还有一个就是地址变量做函数形参。

reverse函数输入pre->顺序序列->end
返回 pre->倒叙序列->end。
函数返回值是end前一个node,作为下一个k部分的pre。

CPP代码如下。注视部分有问题待解决。

class Solution {
public: 
    ListNode* reverseKGroup(ListNode* head, int k) {
        if(head == NULL) return head;  
        ListNode* dummy = new ListNode(0);  
        dummy->next = head;  
        int count = 0;  
        ListNode* pre = dummy;  
        ListNode* cur = head;  
        while(cur != NULL)  
        {  
            count ++; 
            cur=cur->next;
            //ListNode* next = cur->next;  
            if(count == k)  
            {  
                pre = reverse(pre, cur);  
                count = 0;     
            }  
            //cur = next;  
        }  
        return dummy->next;  
    }
    ListNode* reverse(ListNode* pre, ListNode* end)  {  
        if(pre==NULL || pre->next==NULL)  
            return pre;  
        ListNode* head = pre->next;  
        ListNode* cur = pre->next->next;  
        while(cur!=end)  
        {  
            ListNode* next = cur->next;  
            cur->next = pre->next;  
            pre->next = cur;  
            cur = next;  
        }  
        head->next = end;  
        return head;  
        /*ListNode * x=pre->next;
        x->next=NULL;
        ListNode * p = pre->next;//头结点之后的第1个节点
        ListNode * q = pre->next->next;//头结点之后的第2节点
        ListNode * m = pre->next->next->next;//头结点之后的第3个节点
        p->next = NULL;
        while(m!=end){
            q->next = p;
            p = q;
            q = m;
            m = m->next;
        }
        q->next = p;
        x->next=m;
        pre->next = q;
        return x;*/
    }  
};

还有一种方法:

/**
 * 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 *s,*p=(ListNode*)malloc(sizeof(ListNode));
        int len=k;
        p->next=head;head=p;s=p;
        while(k-- && s!=NULL)s=s->next;
        while(s!=NULL){
            reverse(p,s);
            k=len;
            while(k-- && s!=NULL)s=s->next;
        }
        return head->next;
    }
private:
    void reverse(ListNode* &p,ListNode* &s){
        ListNode *tmp,*tail=p->next,*flag=s->next,*l=p->next;
        p->next=NULL;
        while(l!=flag){
            tmp=p->next;
            p->next=l;
            l=l->next;
            p->next->next=tmp;
        }
        tail->next=l;
        p=tail;
        s=tail;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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...

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]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...

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...

leetcode25 Reverse Nodes in k-Group

Reverse Nodes in k-Group My Submissions Question Total Accepted: 47206 Total Submissions:...

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 25:Reverse Nodes in k-Group(C)

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

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

原题网址:https://leetcode.com/problems/reverse-nodes-in-k-group/ Given a linked list, reverse the node...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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