leetcode-25-Reverse Nodes in k-Group

17 篇文章 0 订阅
7 篇文章 0 订阅

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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值