leetcode-25-Reverse Nodes in k-Group

来源:互联网 发布:php跳转页面 编辑:程序博客网 时间:2024/04/30 01:05

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
原创粉丝点击