gpt4 book ai didi

检查单向链表是否为回文 C

转载 作者:行者123 更新时间:2023-12-02 03:14:43 24 4
gpt4 key购买 nike

我正在尝试检查单向链表是否为回文。约束是 - 算法必须在线性时间和恒定空间内。

我使用的基本算法如下 -

  1. 使用快慢指针将列表分成两半。
  2. 原地逆转下半场。
  3. 比较上半场和下半场。
  4. 构造回原始列表
  5. 返回结果。

我的实现适用于列表中元素数量为偶数但元素数量为奇数时失败。

/*
* @brief Checks if a list is a palindrome or not
*/
bool is_palindrome(node *head)
{
node *first; /* Points to head node of 1st half */
node *second; /* Points to head node of 2nd half */
node *f_ptr = head; /* Fast pointer */
node *s_ptr = head; /* Slow pointer */
node *prev = NULL; /* Previous to slow pointer */
bool ret = false; /* Return value */

while (f_ptr && f_ptr->next && f_ptr->next->next)
{
prev = s_ptr;
s_ptr = s_ptr->next;
f_ptr = f_ptr->next->next;
}

/* List with even number of elements */
if (!(f_ptr->next->next))
{
first = head;
second = s_ptr->next;
s_ptr->next = NULL;
/* Reverse the second half */
second = reverse_list(&second);
/* Compare the first & second half */
ret = are_identical(first, second);
/* Finally, construct the original list back */
second = reverse_list(&second);
s_ptr->next = second;
print_list(head);
}
/* List with odd number of elements */
if (!(f_ptr->next))
{
first = head;
second = s_ptr->next;
prev->next = NULL;
s_ptr->next = NULL;
/* Reverse the second half */
second = reverse_list(&second);
/* Compare the first & second half */
ret = are_identical(first, second);
/* Finally, construct the original list back */
second = reverse_list(&second);
prev->next = s_ptr; s_ptr->next = second;
print_list(head);
}
return ret;
}

有人可以帮我弄清楚我在处理奇数案件时做错了什么吗?可以找到此程序的完整实现 here .

最佳答案

感谢s7amuser指出错误。以下是适用于偶数和奇数情况的实现。

/*
* @brief Checks if a list is a palindrome or not
*/
bool is_palindrome(node *head)
{
node *first; /* Points to head node of 1st half */
node *second; /* Points to head node of 2nd half */
node *f_ptr = head; /* Fast pointer */
node *s_ptr = head; /* Slow pointer */
node *prev = NULL; /* Previous to slow pointer */
bool ret = false; /* Return value */

while (f_ptr && f_ptr->next && f_ptr->next->next)
{
prev = s_ptr;
s_ptr = s_ptr->next;
f_ptr = f_ptr->next->next;
}

/* List with odd number of elements */
if (!(f_ptr->next))
{
first = head;
second = s_ptr->next;
prev->next = NULL;
s_ptr->next = NULL;
/* Reverse the second half */
second = reverse_list(&second);
/* Compare the first & second half */
ret = are_identical(first, second);
/* Finally, construct the original list back */
second = reverse_list(&second);
prev->next = s_ptr; s_ptr->next = second;
print_list(head);
}
/* List with even number of elements */
else if (!(f_ptr->next->next))
{
first = head;
second = s_ptr->next;
s_ptr->next = NULL;
/* Reverse the second half */
second = reverse_list(&second);
/* Compare the first & second half */
ret = are_identical(first, second);
/* Finally, construct the original list back */
second = reverse_list(&second);
s_ptr->next = second;
print_list(head);
}
return ret;
}

关于检查单向链表是否为回文 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com