链表K个节点翻转

本文介绍了一种链表操作方法,重点在于实现链表的创建、插入、打印及反转功能,并特别关注如何按每k个节点一组进行反转。通过具体代码示例详细解释了这些操作的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>  
using namespace std;  
  
struct ListNode  
{  
    int m_nValue;  
    ListNode *m_pNext;  
};  
  
ListNode* CreateList(int val)  
{  
    ListNode *pHead = new ListNode;  
  
    pHead->m_nValue = val;  
    pHead->m_pNext = NULL;  
  
    return pHead;  
}  
  
void InsertNode(ListNode **pHead, int val)  
{  
    ListNode *pNode = new ListNode;  
    pNode->m_nValue = val;  
    pNode->m_pNext = NULL;  
  
    while ((*pHead)->m_pNext != NULL)  
    {  
        (*pHead) = (*pHead)->m_pNext;  
    }  
  
    (*pHead)->m_pNext = pNode;  
    (*pHead) = pNode;  
}  
  
void PrintList(ListNode *pHead)  
{  
    while (pHead != NULL)  
    {  
        cout<<pHead->m_nValue<<" ";  
        pHead = pHead->m_pNext;  
    }  
    cout<<endl;  
}  
  
ListNode* Reverse(ListNode *pHead)  
{  
    if (pHead == NULL || pHead->m_pNext == NULL)  
    {  
        return pHead;  
    }  
  
    ListNode *pPre = NULL;  
    ListNode *pCurrent = pHead;  
    ListNode *pPost = pHead->m_pNext;  
  
    while (pCurrent->m_pNext != NULL)  
    {  
        pCurrent->m_pNext = pPre;  
        pPre = pCurrent;  
        pCurrent = pPost;  
        pPost = pPost->m_pNext;  
    }  
    pCurrent->m_pNext = pPre;  
  
    return pCurrent;  
}  
  
  
  
ListNode* ReverseList(ListNode *pHead, int k)  
{  
    if (pHead==NULL || pHead->m_pNext==NULL)  
    {  
        return pHead;  
    }  
  
    ListNode *pPre = NULL;  
    ListNode *pCurrent = pHead;  
    ListNode *pPost = pHead->m_pNext;  
    ListNode *pStart = NULL;  
    ListNode *pEnd = NULL;  
  
    int n = 0;  
    pEnd = pCurrent;  
    pEnd->m_pNext = NULL;  
    while (pPost != NULL)  
    {  
        ++n;  
        if (n == (k+1))  
        {  
            pStart = pPre;  
            pEnd->m_pNext = ReverseList(pCurrent, k);  
  
            return pStart;  
        }  
        else  
        {  
            pCurrent->m_pNext = pPre;  
            pPre = pCurrent;  
            pCurrent = pPost;  
            pPost = pPost->m_pNext;  
        }  
    }
  
    pCurrent->m_pNext = pPre;  
	pStart = pCurrent;
    return pStart;  
}  
  
int main()  
{  
    ListNode *pHead = NULL;  
    ListNode *head = NULL;  
    int n;  
    cout<<"输入链表中节点的个数 n:"<<endl;  
    cin>>n;  
    cout<<"请输入n个整数值:"<<endl;  
    for (int i=0; i<n; ++i)  
    {  
        int data;  
        cin>>data;  
  
        if (pHead == NULL)  
        {  
            pHead = CreateList(data);  
            head = pHead;  
        }  
        else  
        {  
            InsertNode(&pHead, data);  
        }  
    }  
  
    int k;  
    cout<<"请输入k:"<<endl;  
    cin>>k;  
    head = ReverseList(head, k);  
    PrintList(head);  
  
    system("pause");  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值