【单链表】链表反转

1、需求

在这里插入图片描述

2、思路

  主要原理就是采用头插法,创建一个新链表头结点,将原链表的每个结点依次插入到新链表的头结点位置,即从后往前插入,最后返回新的链表头结点   //新链表指的是原链表反转之后所形成的链表。
         在这里插入图片描述

3、实现步骤

3.1 创建3个结点指针

   (1)首先创建一个newHead 空指针来作为新链表的头结点
   (2)其次创建一个currentNode结点指针,用于表示当前结点。//默认第一次指向原链表的头结点pHead
   (3)最后创建一个nextNode结点指针,用于保存当前结点的下一个结点。//原因是头插法需要改变当前结点currentNode的下一个结点的指向,改变之前,需要先对它的下一个结点进行保存,否则就会失去链表原有的联系,也即头被嘎了,找不到下一个是谁

struct ListNode *newHead = NULL, *currentNode = pHead, *nextNode = NULL;

3.2 按照头插法,从后往前插入原链表每个结点

nextNode = current->next;//因为current->next要指向新链表的头结点,所以先保存原来current->next的指向
current->next = newHead;//令current->next指向新链表的头结点
newHead = current;//令新链表头结点指向插入的新结点
currentNode = nextNode;//令currentNode指向最先保存好的下一个结点

4、反转链表代码

 /* struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */

/**
 * 
 * @param pHead ListNode类 
 * @return ListNode类
 */
struct ListNode* ReverseList(struct ListNode* pHead ) 
{
	struct ListNode *newHead = NULL, *currentNode = pHead, *nextNode = NULL;
    if(pHead == NULL)
    {
        printf("The LinkList is empty!\n");
        return NULL;
    }
    while(currentNode)
    {
        nextNode = currentNode->next;
        currentNode->next = newHead;
        newHead = currentNode;
        currentNode = nextNode;
    }
    return newHead;
}

5、完整代码

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

struct ListNode {
        int val;
        struct ListNode *next;
};
struct ListNode* newNode()
{
        struct ListNode* p;
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        printf("input value of new node:");
        scanf("%d", &p->val);
        p->next = NULL;
        printf("已创建该节点\n");
        return p;
}
struct ListNode* buildList()
{
        struct ListNode* head = NULL, *p = NULL, *phead = NULL;
        int count = 0;
        printf("请输入链表的节点个数:");
        scanf("%d", &count);
        while(count--)
        {
                p = (struct ListNode*)malloc(sizeof(struct ListNode));
                p = newNode();
                if(head == NULL)
                {
                        phead = p;
                        head = phead;//固定头部;
                }else{
                        phead->next = p;
                        phead = phead->next;
                }
        }
        printf("---创建链表完成---\n");
        return head;
}
void printList(struct ListNode *head)
{
        struct ListNode* phead;
        phead = head;
        printf("链表节点值:");
        while(phead != NULL)
        {
                printf("%d ", phead->val);
                phead = phead->next;
        }
        printf("\n");
}

//头插法
struct ListNode* ReverseList_head(struct ListNode* pHead )
{
        struct ListNode *newHead = NULL,*currentNode = pHead, *nextNode = NULL;
        while(currentNode != NULL)
        {
                nextNode = currentNode->next;
                currentNode->next = newHead;
                newHead = currentNode;
                currentNode = nextNode;
        }
        return newHead;
}
void main()
{
        struct ListNode* head;
        head = (struct ListNode*)malloc(sizeof(struct ListNode));
        head = buildList();
        head = ReverseList_head(head);
        printList(head);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值