T7 链表的回文(对称)结构
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode* slow,*fast;
slow = fast = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* newhead = NULL;
struct ListNode* cur = head;
while (cur)
{
struct ListNode* next = cur->next;
cur->next = newhead;
newhead = cur;
cur = next;
}
return newhead;
}
int isPalindrome(struct ListNode* head)
{
struct ListNode* mid = middleNode(head);
struct ListNode* rhead = reverseList(mid);
while (head && rhead)
{
if (head->val != rhead->val)
{
return 0;
}
else
{
head = head->next;
rhead = rhead->next;
}
}
return 1;
}
int main()
{
struct ListNode* n1=(struct ListNode*)malloc(sizeof(struct ListNode));
assert(n1);
struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n2);
struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n3);
struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n4);
struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n5);
struct ListNode* n6 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n6);
struct ListNode* n7 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n7);
n1->val = 1;
n2->val = 2;
n3->val = 3;
n4->val = 4;
n5->val = 3;
n6->val = 2;
n7->val = 1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
n5->next = n6;
n6->next = n7;
n7->next = NULL;
int ret = isPalindrome(n1);
if (1==ret)
{
printf("该链表是回文结构\n");
}
else
{
printf("该链表不是回文结构\n");
}
free(n1);
n1 = NULL;
free(n2);
n2 = NULL;
free(n3);
n3 = NULL;
free(n4);
n4 = NULL;
free(n5);
n5 = NULL;
free(n6);
n6 = NULL;
free(n7);
n7 = NULL;
return 0;
}
T8 相交链表
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
{
struct ListNode* curA = headA, * curB = headB;
int lenA = 1, lenB = 1;
while (curA->next)
{
curA = curA->next;
lenA++;
}
while (curB->next)
{
curB = curB->next;
lenB++;
}
if (curA != curB)
{
return NULL;
}
struct ListNode* shortList = headA, * longList = headB;
if (lenA > lenB)
{
shortList = headB;
longList = headA;
}
int gap = abs(lenA-lenB);
while (gap--)
{
longList = longList->next;
}
while (longList != shortList)
{
longList = longList->next;
shortList = shortList->next;
}
return longList;
}
int main()
{
struct ListNode* n1=(struct ListNode*)malloc(sizeof(struct ListNode));
assert(n1);
struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n2);
struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n3);
struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n4);
struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(n5);
n1->val = -2;
n2->val = -1;
n3->val = 0;
n4->val = 2;
n5->val = 3;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
n5->next = NULL;
struct ListNode* m1 = (struct ListNode*)malloc(sizeof(struct ListNode));
assert(m1);
m1->val = 1;
m1->next = n4;
struct ListNode* internode = getIntersectionNode(n1,m1);
printf("%d\n",internode->val);
free(n1);
n1 = NULL;
free(n2);
n2 = NULL;
free(n3);
n3 = NULL;
free(n4);
n4 = NULL;
free(n5);
n5 = NULL;
free(m1);
m1 = NULL;
return 0;
}