# 力扣 - 234、回文链表

### 题目

输入: 1->2

输入: 1->2->2->1

### 分析

C

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node LNode;
struct node
{
int data;
LNode* next;
};
{
LNode* q = NULL;
LNode* p = NULL;
int size = 0;
{
perror("申请空间失败");
exit(EXIT_FAILURE);
}
printf("请输入需要添加的节点个数:");
scanf("%d", &size);
printf("请依次输入节点的值(用空格隔开):\n");
while (size > 0)
{
p = (LNode*)malloc(sizeof(LNode));//申请子节点空间
scanf("%d", &p->data);
p->next = NULL;//尾插入
q->next = p;
q = p;
size--;
}
}
{
int len = 0;
while (NULL != p)
{
len++;
p = p->next;
}
return len;
}
{
{
}
}
{
while (NULL != p)
{
printf("%d ", p->data);
p = p->next;
}
}
//判断链表是否为回文链表
//是回文返回true  不是回文返回false
{
int mark = true;  //记录是否回文
int* n = (int*)malloc(sizeof(int) * len);
int i = 0;
while (NULL != p)//遍历链表 存入数组
{
n[i] = p->data;
p = p->next;
i++;
}
int start = 0;    //前指针
int end = len - 1;  //后指针
while (start < end)
{
if (n[start] != n[end])//一次不等直接跳出。
{
mark = false;
break;
}
start++;
end--;
}
return mark;
}
int main()
{
if (ret == true)
{
printf("该链表是回文链表！\n");
}
else
{
printf("该链表不是回文链表！\n");
}
return 0;
}

LNode* fast = head;//快指针
/*
* *如果是奇数列  找最中间的
* *如果是偶数列  找左边的中间值
*/
while (fast->next != NULL && fast->next->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}

LNode* front = NULL;    //前指针
LNode* present = slow->next;//需要反转的节点
LNode* later = present;   //后指针
while (present != NULL)   //反转后半部分
{
later = later->next;
present->next = front;
front = present;
present = later;
}

front = head; //前指针
later = front;  //后指针
/*判断回文*/
while (later != NULL)//后指针走到空为止
{
if (front->data != later->data)
{
mark = false;
}
front = front->next;
later = later->next;
}

/*复原链表*/
later = NULL; //后指针
present = fast; //需要反转的节点   最右边
front = present;//前指针
while (front != NULL)
{
front = front->next;
present->next = later;
later = present;
present = front;
}

C

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node LNode;
struct node
{
int data;
LNode* next;
};
{
LNode* q = NULL;
LNode* p = NULL;
int size = 0;
{
perror("申请空间失败");
exit(EXIT_FAILURE);
}
printf("请输入需要添加的节点个数:");
scanf("%d", &size);
printf("请依次输入节点的值(用空格隔开):\n");
while (size > 0)
{
p = (LNode*)malloc(sizeof(LNode));//申请子节点空间
scanf("%d", &p->data);
p->next = NULL;//尾插入
q->next = p;
q = p;
size--;
}
}
{
int len = 0;
while (NULL != p)
{
len++;
p = p->next;
}
return len;
}
{
{
}
}
{
while (NULL != p)
{
printf("%d ", p->data);
p = p->next;
}
}
//判断链表是否为回文链表
//是回文返回true  不是回文返回false
{
bool mark = true;
/*
* *如果是奇数列  找最中间的
* *如果是偶数列  找左边的中间值
*/
while (fast->next != NULL && fast->next->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
/*
* *如果快指针的下一个不为空。就再走一步
*/
if (fast->next != NULL)
{
fast = fast->next;
}
LNode* front = NULL;    //前指针
LNode* present = slow->next;//需要反转的节点
LNode* later = present;   //后指针
while (present != NULL)   //反转后半部分
{
later = later->next;
present->next = front;
front = present;
present = later;
}
later = front;  //后指针
/*判断回文*/
while (later != NULL)//后指针走到空为止
{
if (front->data != later->data)
{
mark = false;
}
front = front->next;
later = later->next;
}
/*复原链表*/
later = NULL; //后指针
present = fast; //需要反转的节点   最右边
front = present;//前指针
while (front != NULL)
{
front = front->next;
present->next = later;
later = present;
present = front;
}
return mark;
}
int main()
{
if (ret == true)
{
printf("该链表是回文链表！\n");
}
else
{
printf("该链表不是回文链表！\n");
}
return 0;
}

LNode* front = NULL;  //前指针
//递归判断回文
//有一次false就是false。只有都是true才是true
{
{
{
return false;
}
{
return false;
}
front = front->next;//前指针往后走
}
return true;
}
//判断链表是否为回文链表
//是回文返回true  不是回文返回false
{
}


C

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node LNode;
struct node
{
int data;
LNode* next;
};
{
LNode* q = NULL;
LNode* p = NULL;
int size = 0;
{
perror("申请空间失败");
exit(EXIT_FAILURE);
}
printf("请输入需要添加的节点个数:");
scanf("%d", &size);
printf("请依次输入节点的值(用空格隔开):\n");
while (size > 0)
{
p = (LNode*)malloc(sizeof(LNode));//申请子节点空间
scanf("%d", &p->data);
p->next = NULL;//尾插入
q->next = p;
q = p;
size--;
}
}
{
int len = 0;
while (NULL != p)
{
len++;
p = p->next;
}
return len;
}
{
{
}
}
{
while (NULL != p)
{
printf("%d ", p->data);
p = p->next;
}
}
LNode* front = NULL;  //前指针
//递归判断回文
//有一次false就是false。只有都是true才是true
{
{
{
return false;
}
{
return false;
}
front = front->next;//前指针往后走
}
return true;
}
//判断链表是否为回文链表
//是回文返回true  不是回文返回false
{
}
int main()
{
if (ret == true)
{
printf("该链表是回文链表！\n");
}
else
{
printf("该链表不是回文链表！\n");
}
return 0;
}

|
28天前
【bug记录】旋转链表与力扣报错：member access within null pointer of type ‘struct ListNode‘
【bug记录】旋转链表与力扣报错：member access within null pointer of type ‘struct ListNode‘
23 0
|
27天前
|

LeetCode第24题两两交换链表中的节点

27 3
|
26天前
|

LeetCode第86题分隔链表

24 2
|
26天前
|

LeetCode第83题删除排序链表中的重复元素

26 2
|
27天前
|

LeetCode第23题合并 K 个升序链表

26 2
|
14天前
|
C++ 索引
leetcode 707.设计链表

23 1
|
26天前
|

LeetCode第92题反转链表 II

44 0
|
27天前
|

LeetCode第21题合并两个有序链表

24 0
|
27天前
|

LeetCode第19题删除链表的倒数第 N 个结点

22 0
|
28天前
【刷题记录】链表的回文结构
【刷题记录】链表的回文结构
11 1