【ACM模式】链表输入、链表打印、反转链表、反转链表前n个、链表内指定区间反转

本文介绍了如何使用C++实现链表的区间反转和特定部分的链表反转操作,通过`Solution`类展示了`reverseBetween`和`reverseL`函数的实现,包括`InputList`、`ReverseList`和`PrintList`辅助函数。实例代码详细讲解了如何构建链表并观察反转前后效果。
摘要由CSDN通过智能技术生成

C++版本

#include<iostream>
using namespace std;

struct ListNode {
    int val;
    struct ListNode *next;
};

//链表内指定区间反转
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if (m == 1) return reverseL(head, n);  // 如果m为1,那么就是反转前n个链表的解法
        head->next = reverseBetween(head->next, m - 1, n - 1);
        return head;
    }

    ListNode* behind = NULL;  // 后继节点
    ListNode* reverseL(ListNode* head, int L)  // 反转前L个链表
    {
        if (L == 1) {
            behind = head->next;
            return head;
        }
        ListNode* newHead = reverseL(head->next, L - 1);
        head->next->next = head;
        head->next = behind;
        return newHead;
    }
};

//输入并创建链表
ListNode *InputList()
{
    int n, temp;
    ListNode *pHead = new ListNode();
    ListNode *cur = pHead;
    cin >> n;  //链表长度n
    for (int i = 1; i <= n; i++) {
        cin >> temp;
        if (i == 1) {
            cur->val = temp;
            cur->next = NULL;
        } else {
            ListNode *newNode = new ListNode();
            newNode->val = temp;
            newNode->next = NULL;
            cur->next = newNode;
            cur = newNode;
        }
    }
    return pHead;
}
//翻转链表
ListNode *ReverseList(ListNode *head)
{
    if (head == nullptr || head->next == nullptr) return head;
    ListNode *newHead = ReverseList(head->next);
    head->next->next = head;
    head->next = nullptr;
    return newHead;
}
//打印链表
void PrintList(ListNode *head)
{
    while (head) {
        cout << head->val;
        if (head->next) cout << " -> ";
        head = head->next;
    } cout << endl;
}

int main() {
    ListNode *head = InputList();
    cout << "构造链表如下:" << endl;
    PrintList(head);

    head = ReverseList(head);
    cout << "翻转链表后" << endl;
    PrintList(head);

    Solution s;
    head = s.reverseBetween(head,1,2);
    cout << "掐头去尾链表反转后" << endl;
    PrintList(head);

    head = s.reverseL(head,2);
    cout << "前2个链表反转后" << endl;
    PrintList(head);
    return 0;
}

/*
输入
5
6 3 1 1 2
*/

C版本

#include <stdio.h>
#include <stdlib.h>

#define N 5
typedef struct node {
    int data;
    struct node *next;
} NODE;

NODE *ReverseList(NODE *h) {
    if(h == NULL || h->next == NULL) return h;
    NODE *newNode = ReverseList(h->next);
    h->next->next = h;
    h->next = NULL;
    return newNode;
}

NODE *creatlistnew(int a[]) {
    NODE *pHead = (NODE *) malloc(sizeof(NODE));;
    NODE *p = pHead;
    for (int i = 0; i < N; i++) {
        if (i == 0) {
            p->data = a[i];
            p->next = NULL;
        }
        else{
            NODE *newNode = (NODE *) malloc(sizeof(NODE));
            newNode->data = a[i];
            newNode->next = NULL;
            p->next = newNode;
            p = newNode;
        }
    }
    return pHead;
}

NODE *creatlist(int a[]) {
    NODE *h, *p, *q;
    int i;
    h = NULL;
    for (i = 0; i < N; i++) {
        q = (NODE *) malloc(sizeof(NODE));
        q->data = a[i];
        q->next = NULL;
        if (h == NULL)
            h = p = q;
        else{
            p->next = q;
            p = q;
        }
    }
    return h;
}

void outlist(NODE *h){
    NODE *p = h;
    if(p == NULL)
        printf("NULL\n");
    else{
        printf("Head");
        do{
            printf("->%d",p->data);
            p = p->next;
        } while (p!=NULL);
        printf("->End\n");
    }
}
int main() {
    NODE *head;
    int a[N] = {2,4,6,8,10};
//    head = creatlistnew(a);
    head = creatlistnew(a);
    printf("原始链表\n");
    outlist(head);
    head = fun(head);
    printf("反转链表\n");
    outlist(head);
    return 0;
}

/*
输出:
原始链表
Head->2->4->6->8->10->End
反转链表
Head->10->8->6->4->2->End
*/

反转链表
算法备忘录~翻转单链表(ACM模式)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值