1 Star 1 Fork 0

LC.yulin/数据结构

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.8_4.c 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
LC.yulin 提交于 2022-08-04 20:58 . 数据结构链表oj题
class PalindromeList {
public:
struct ListNode* middleNode(struct ListNode* head) {
struct ListNode* slow = head;
struct ListNode* fast = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* n1 = NULL;
struct ListNode* n2 = head;
struct ListNode* n3 = NULL;
while (n2)
{
n3 = n2->next;
n2->next = n1;
n1 = n2;
n2 = n3;
}
return n1;
}
bool chkPalindrome(ListNode* A) {
// write code here
struct ListNode* mid = middleNode(A);
struct ListNode* rmid = reverseList(mid);
while (A && rmid)
{
if (A->val != rmid->val)
return false;
else
{
A = A->next;
rmid = rmid->next;
}
}
return true;
}
};
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
// write code here
struct ListNode* lessGuard, * lessTail, * graterGuard, * graterTail;
lessGuard = lessTail = (struct ListNode*)malloc(sizeof(struct ListNode));
graterGuard = graterTail = (struct ListNode*)malloc(sizeof(struct ListNode));
lessGuard->next = NULL;
graterGuard->next = NULL;
struct ListNode* cur = pHead;
while (cur)
{
if (cur->val < x)
{
lessTail->next = cur;
lessTail = lessTail->next;
}
else
{
graterTail->next = cur;
graterTail = graterTail->next;
}
cur = cur->next;
}
lessTail->next = graterGuard->next;
graterTail->next = NULL;
pHead = lessGuard->next;
free(graterGuard);
free(lessGuard);
return pHead;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/lc-yulin/data-structure.git
git@gitee.com:lc-yulin/data-structure.git
lc-yulin
data-structure
数据结构
master

搜索帮助