代码拉取完成,页面将自动刷新
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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。