#2
rjsp2023-12-25 14:54
|
先放代码:
程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
typedef struct ListNode* ptr;
struct ListNode {
int val;
struct ListNode* next;
};
void ReverseList(struct ListNode* L)
{
ptr p = findptr(L);
ptr head = L;
while (p != head)
{
//反转
p->next->next = p;
p->next = NULL;
//更新p
p = findptr(L), head = L;
}
if (head->next != NULL) {
p->next->next = p;
p->next = NULL;
}
/////////////////////////////
}
struct ListNode* findptr(struct ListNode* L)
{
ptr head = L;
ptr p = head, temp = p;
if (p->next != NULL) temp = p->next;
//找最后
while (temp->next != NULL)
{
p = p->next;
temp = p->next;
}
return p;
}
int main() {
//
ptr l1;
//l1
ptr p = malloc(sizeof(struct ListNode)), temp = NULL;
l1 = p;
if (p == NULL) {
return 1;
}
p->val = 2;
temp = malloc(sizeof(struct ListNode));
if (temp == NULL) {
return 1;
}
temp->val = 4;
p->next = temp;
temp->next = NULL;
////
ReverseList(l1);
p = findptr(l1);
printf("%d ",p->next->val );
return 0;
}
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
typedef struct ListNode* ptr;
struct ListNode {
int val;
struct ListNode* next;
};
void ReverseList(struct ListNode* L)
{
ptr p = findptr(L);
ptr head = L;
while (p != head)
{
//反转
p->next->next = p;
p->next = NULL;
//更新p
p = findptr(L), head = L;
}
if (head->next != NULL) {
p->next->next = p;
p->next = NULL;
}
/////////////////////////////
}
struct ListNode* findptr(struct ListNode* L)
{
ptr head = L;
ptr p = head, temp = p;
if (p->next != NULL) temp = p->next;
//找最后
while (temp->next != NULL)
{
p = p->next;
temp = p->next;
}
return p;
}
int main() {
//
ptr l1;
//l1
ptr p = malloc(sizeof(struct ListNode)), temp = NULL;
l1 = p;
if (p == NULL) {
return 1;
}
p->val = 2;
temp = malloc(sizeof(struct ListNode));
if (temp == NULL) {
return 1;
}
temp->val = 4;
p->next = temp;
temp->next = NULL;
////
ReverseList(l1);
p = findptr(l1);
printf("%d ",p->next->val );
return 0;
}
这边的问题在于findptr这个函数在ReverseList()这个函数中使用时,会出现:错误 C2040 “findptr”:“ListNode *(ListNode *)”与“int ()”的间接寻址级别不同 linklist1的问题。
然后我这边考虑到时我findptr这个函数返回出了问题,可是检查后没发现问题。但当我删去ReverseList()这个函数,单独在主函数中使用findptr()时,却可以正常使用,请问这是怎么回事?