【C语言】链表内指定区间反转

问题描述

在这里插入图片描述

核心代码

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
struct ListNode* reverseBetween(struct ListNode* pHead, int m, int n ) {
    /*
        check input error
    */
    if(m>n){
        printf("m can not greater than b");
        exit(0);
    }
    else if(m > 1000){
        printf("m can not greater than 1000");
        exit(0);
    }
    else if(n > 1000){
        printf("n can not greater than 1000");
        exit(0);
    }
    
    struct ListNode* testNode = pHead;
    
    int sum = 0;
    do{
        if(testNode->val > 1000){
            printf("node val can not greater than 1000");
            exit(0);            
        }
        testNode = testNode->next;
        sum ++;
    }
    while(testNode != NULL);
    
    if(sum > 1000){
         printf("chian length can not greater than 1000");
         exit(0); 
    }
    /*
        main code
     */
    if(pHead == NULL || pHead->next == NULL || m == n)
        return pHead;
    
    struct ListNode* before_startNode = NULL;
    struct ListNode* startNode = pHead;
    struct ListNode* endNode = pHead;
    
    //find the start and the end of the interval
    for(int i = 1;i<m;i++){
        before_startNode = startNode;
        startNode = startNode->next;
    }
    for(int i = 1;i<n;i++){
        endNode = endNode->next;
    }
    
    struct ListNode* beforeNode = startNode;
    struct ListNode* Node = startNode->next;
    struct ListNode* afterNode = startNode->next->next;
    
    //handle the begin and the end
    if(pHead==startNode)
        pHead = endNode;
    else
        before_startNode->next = endNode;
    if(endNode->next == NULL);
    
        
    //only 2 elements in this interval
    if(Node == endNode){
        startNode->next = endNode->next;
        endNode->next = startNode;
    }
    //inverse the interval
    for(int i = 1;i<n-m;i++){
        //reach the end of this ineterval
        if(afterNode == endNode){
            startNode->next = endNode->next;
            endNode->next = Node;            
        }
        
        //do the inversion
        Node->next = beforeNode;
        
        //panning the nodes
        beforeNode = Node;
        Node = afterNode;
        afterNode = afterNode->next;
    }

    return pHead;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常用的数据结构,它由一系列的节点组成,每个节点都包含了一个数据元素及一个指向下一个节点的指针。 要在C语言中插入链表指定位置,首先需要定义链表节点的结构体,包括数据元素和指向下一个节点的指针。然后创建新节点,并赋值给新节点的数据元素。接下来,需要遍历链表,找到要插入的位置的前一个节点,并将新节点的指针指向该位置原来的后一个节点。最后,将前一个节点的指针指向新节点,完成插入操作。 具体的插入操作可以用以下代码实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点的结构体 typedef struct Node { int data; struct Node* next; } Node; // 插入指定位置的节点 void insertAtPosition(Node** head, int position, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); // 创建新节点 newNode->data = value; // 给新节点赋值 // 如果插入位置是链表头部 if (position == 0) { newNode->next = *head; *head = newNode; return; } Node* current = *head; int i; // 遍历链表找到要插入位置的前一个节点 for (i = 0; current != NULL && i < position - 1; i++) { current = current->next; } // 如果找不到要插入的位置,返回错误 if (current == NULL) { printf("插入位置无效.\n"); return; } // 插入新节点 newNode->next = current->next; current->next = newNode; } // 遍历链表并打印节点的值 void printList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { Node* head = NULL; // 链表头部 // 在链表尾部插入节点 insertAtPosition(&head, 0, 1); insertAtPosition(&head, 1, 3); insertAtPosition(&head, 2, 5); // 打印链表 printf("链表元素:"); printList(head); return 0; } ``` 以上代码通过`insertAtPosition`函数实现了在链表指定位置插入节点,并通过`printList`函数打印链表的元素。在`main`函数中测试了在链表尾部插入节点,并打印链表的元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值