将单链表的每k个节点之间逆序

题目:

给定一个单链表的头结点head,实现一个调整单链表的函数,使得每k个节点之间逆序,如果最后不管k个节点一组,则不调整最后几个节点。

Example:

链表:1->2->3->4->5->6->7->8->nullptr, k = 3

调整后:3->2->1->6->5->4->7->8->nullptr

代码1:

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x),next(nullptr){}
};

ListNode* resign(stack<ListNode* > &s, ListNode* left, ListNode* right)
{
    ListNode* cur = s.top(); s.pop();
    left->next = cur;
    while (!s.empty())
    {
        ListNode* next = s.top(); s.pop();
        cur->next = next;
        cur = next;
    }
    cur->next = right;
    return cur;
}

ListNode* reverseKNode(ListNode *head, int k)
{
    if (head == nullptr || head->next == nullptr || k < 2)
        return head;
    ListNode* newHead = new ListNode(-1);
    newHead->next = head;
    ListNode* pre = newHead;
    ListNode* cur = head;
    stack<ListNode* > s;
    while (cur != nullptr)
    {
        ListNode* next = cur->next;
        s.push(cur);
        if (s.size() == k)
        {
            pre = resign(s, pre, next);
        }
        cur = next;
    }
    return newHead->next;
}

int main()
{
    ListNode *head = new ListNode(1);
    ListNode* node1 = new ListNode(2);
    ListNode* node2 = new ListNode(3);
    ListNode* node3 = new ListNode(4);
    ListNode* node4 = new ListNode(5);
    ListNode* node5 = new ListNode(6);
    ListNode* node6 = new ListNode(7);
    ListNode* node7 = new ListNode(8);
    head->next = node1;
    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = node5;
    node5->next = node6;
    node6->next = node7;
    head = reverseKNode(head, 3);
    while (head != nullptr)
    {
        cout << head->val << " ";
        head = head->next;
    }
    return 0;
}

代码2:

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

void resign(ListNode* left, ListNode* start, ListNode* end, ListNode* right)
{
    ListNode* pre = start;
    ListNode* cur = start->next;
    ListNode* next = nullptr;
    while (cur != right)
    {
        next = cur->next;
        cur->next = pre;
        pre = cur;
        cur = next;
    }
    left->next = end;
    start->next = right;

}

ListNode* reverseKNode(ListNode* head, int k)
{
    if (head == nullptr || head->next == nullptr || k < 2)
        return head;
    ListNode* newHead = new ListNode(-1);
    newHead->next = head;
    ListNode* pre = newHead;
    ListNode* start = head;
    ListNode* cur = head;
    ListNode* next = nullptr;
    int count = 1;
    while (cur != nullptr)
    {
        next = cur->next;
        if (count == k)
        {
            start = pre->next;
            resign(pre, start, cur, next);
            pre = start;
            count = 0;
        }
        ++count;
        cur = next;
    }
    return newHead->next;

参考资料:

左程云著《程序员代码面试指南》

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值