#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#pragma warning(disable:4996)
using namespace std;
struct ListNode {
int val;
struct ListNode* next;
};
void List_TailInsert(ListNode** head)//尾插法建立带有头节点的链表
{
*head = (ListNode*)malloc(sizeof(ListNode));//头节点,不存数据
ListNode* rear = *head;
ListNode* p;
int num;
scanf("%d", &num);
while (num != 9999) {//输入9999表示结束
p = (ListNode*)malloc(sizeof(ListNode));
p->val = num;
rear->next = p;
rear = p;
scanf("%d", &num);
}
rear->next = NULL;
}
int main()
{
printf("输入链表数据(以9999结束):");
ListNode* head;
List_TailInsert(&head);//传头指针的指针,这样不用返回值了就
ListNode* temp = head->next;
printf("遍历链表为:");
while (temp) {
printf("%d ", temp->val);
temp = temp->next;
}
return 0;
}
206. 反转链表(206.1尾插法建立链表)
最新推荐文章于 2025-02-06 11:02:05 发布