文章目录
链表试题
删除链表中的所有元素 x
链表的遍历 遇到数据x删除 只需讨论特殊情况:
1.空链表
2.第一个结点需要删除
void SListRemoveAll(SList* plist, SLTDataType x)//删除链表中所有的元素 x 面试题1
{
assert(plist);
if (plist->_head == NULL)//空链表
return;
SListNode*cur = plist->_head;
SListNode*ptr = NULL;
while (cur->_next != NULL)
{
if (cur->_next->_data == x)
{
ptr = cur->_next->_next;
free(cur->_next);
cur->_next = ptr;
}
else
cur = cur->_next;
}
if (plist->_head->_data == x)//第一个结点需要删除
{
ptr = plist->_head->_next;
free(plist->_head);
plist->_head = ptr;
}
}
删除有序链表中所有相同的元素(不考虑内存泄漏)
p1 表示当前结点 p2表示下一个结点 pre表示前一个结点 因此在第一个结点前创建一个假结点
例:
1.当p1数据和p2不同时pre->next = p1 pre后移 p1后移 p2 后移
2. 若p1和p2数据相同 让p2往后移动 (此时pre和p1都不动)
直到p2为空 或者p2的数据和p1不同 此时 pre和p2之间的元素都是需要删除的 所以 让pre->next 为p2
1.2步骤循环 直到p2 == NULL;
最后让头结点指向 fake->next; 释放fake
SList* RemoveAllSame(SList*plist)//删除有序链表中所有相同的元素
{
assert(plist);
if (plist->_head == NULL)
return NULL;
SListNode*pre = (SListNode*)(malloc(sizeof(SListNode)));
assert(pre);
pre->_next = plist->_head;
SListNode*fake = pre;
SListNode*p1 = NULL;
SListNode*p2 = NULL;
p1 = plist->_head;//第一个结点
p2 = plist->_head->_next;
while (p2 != NULL)
{
//不相等的时候是ptr移动 相等的时候ptr->_next 移动
if (p1->_data != p2->_data)
{
pre = p1;
p1 = p2;
p2 = p2->_next;
}
else
{
while (p2 != NULL&&p1->_data == p2->_data)
{//p1不动 让p2继续 直到p2!=p1
p2 = p2->_next;
}
pre->_next = p2;
p1 = p2;
if (p2 != NULL)
{
p2 = p2->_next;
}
}
}
plist->_head = fake->_next;
free(fake);
return plist;
}
链表中的数据冒泡排序
只是对链表中的数据进行改变
冒泡排序:循环内部再进行一次循环
用tail来表示链表的末端 每进行一次排序 tail向前移动一个
cur表现第一个结点
next表示下一个结点 让cur和next经行比较 每比较一次cur和next往后移动一位 直到进行到尾部的tail
}
void BubbleSort(SList*plist)//冒泡排序
{
assert(plist);
if (plist->_head == NULL)
return;
SListNode*tail = NULL;
while (plist->_head != tail)
{
SListNode*cur = plist->_head;
SListNode*next = cur->_next;
while (next != tail)
{
if (cur->_data > next->_data)
{
SLTDataType temp = cur->_data;
cur->_data = next->_data;
next->_data = temp;
}
cur = cur->_next;
next = next->_next;
}
tail = cur;
}
}
返回中间结点
用两个指针同时移动 一个每次移动一个结点 一个每次移动两个结点
SListNode* MiddleNode(SList*plist)//返回中间结点 快慢指针
{
assert(plist);
if (plist->_head == NULL)
return NULL;
SListNode*fast = plist->_head;
SListNode*slow = plist->_head;
while (fast->_next != NULL)
{
fast = fast->_next;
slow = slow->_next;
if (fast->_next != NULL)
fast = fast->_next;
}
return slow;
}
返回倒数第k个结点
两个指针 fast先提前移动 k个结点 。
slow移动一个结点 然后fast移动一个结点 当fast为NULL时 slow为目标结点
特殊情况:
1.k大于总结点个数
2.k刚好等于结点个数
SListNode* FindKthTotail(SList*plist, SLTDataType k)//返回倒数第k个结点
{
assert(plist != NULL);
if (plist->_head == NULL)
return NULL;
SListNode*fast = plist->_head;
SListNode*slow = plist->_head;
while (k-1)//先移动k-1此
{//k等于总个数的情况
fast = fast->_next;
if (fast == NULL)
return NULL;
k--;
}
fast = fast->_next;再移动一次,若刚好等于 直接返回slow
while (fast != NULL)
{
fast = fast->_next;
slow = slow->_next;
}
return slow;
}
判断链表是否有环
单链表中只有一个指针域,若有环一定是最后一个结点 不再指向NULL, 而是指向其他结点。
方法:
两个指针一快一慢,慢的每次移动一个结点,快的每次移动两个结点。若有环,快慢指针一定会相遇。
因为快指针移动两个结点,所以要保证 fast->_next != NULL。
让两个指针每次走完都判断一次是否相等,若相等则有环 ,当快指针fast->next ==NULL说明没有环
int IsCircleSList(SList*plist)//判断链表是否有环
{
assert(plist);
SListNode*fast = plist->_head;
SListNode*slow = plist->_head;
while (fast&&fast->_next)
{
if (fast->_next->_next == NULL)
return -1;
fast = fast->_next->_next;
if (fast == slow)
return 1;
slow = slow->_next;
if (fast == slow)
return 1;
}
return -1;
}
判断是否有环时 快慢指针的相遇点
SListNode* MeetNode(SList*plist)
{
assert(plist);
if (IsCircleSList(plist) < 0)//若链表不带环 直接返回null
return NULL;
SListNode*fast = plist->_head->_next->_next;
SListNode*slow = plist->_head;
while (fast != slow)//找相遇点
{
fast = fast->_next->_next;
if (fast == slow)
return fast;
slow = slow->_next;
if (fast == slow)
return fast;
}
}
链表有环时 找环的入口点
fast 走过的总结点个数为 slow 的二倍。
SListNode*NodeEntrance(SList*plist)
{
assert(plist);
if (IsCircleSList(plist) < 0)
return NULL;
SListNode*cur = plist->_head;
SListNode*meet = MeetNode(plist);
while (cur != meet)
{
cur = cur->_next;
meet = meet->_next;
}
return cur;
}
求两条链表的交点
求出两条链表的长度差x,两个指针分别指向两链表的第一个结点,让长链表的指针先走x个结点 ,然后同时走 ,
两指针相等时即是交点。
int SListLength(SList*plist)//求链表长度
{
assert(plist);
int length = 0;
for (SListNode*cur = plist->_head; cur != NULL; cur = cur->_next)
{
length++;
}
return length;
}
SListNode* SListMeetNode(SList*plist, SList*qlist)//求两条相交链表的交点
{
assert(plist);
assert(qlist);
SList*longer = plist;
SList*shorter = qlist;
int plength = SListLength(plist);
int qlength = SListLength(qlist);
int diff = 0;
if (plength > qlength)
{
diff = plength - qlength;
}
else
{
diff = qlength - plength;
longer = qlist;
shorter = plist;
}
while (diff)
{
longer->_head = longer->_head->_next;
diff--;
}
while (shorter->_head)
{
if (shorter->_head == longer->_head)
return longer->_head;
else
{
longer->_head = longer->_head->_next;
shorter->_head = shorter->_head->_next;
}
}
return NULL;
}
合并两条 有序链表
SList* MergeTwoList(SList*plist, SList*qlist)//合并两条有序链表
{
assert(plist&&qlist);
SListNode*tail = NULL ;
SListNode*newnode = NULL;
if (plist->_head == NULL)//一条链表为空
return plist;
if (qlist->_head == NULL)//一条链表为空
return qlist;
if (plist == qlist)
return NULL;
if (plist->_head->_data < qlist->_head->_data)
{
newnode = plist->_head;
plist->_head = plist->_head->_next;
}
else
{
newnode = qlist->_head;
qlist->_head = qlist->_head->_next;
}
tail = newnode;
while (plist->_head != NULL&&qlist->_head != NULL)
{
if (plist->_head->_data < qlist->_head->_data)
{
tail->_next = plist->_head;
plist->_head = plist->_head->_next;
}
else
{
tail->_next = qlist->_head;
qlist->_head = qlist->_head->_next;
}
tail = tail->_next;
}
if (plist->_head == NULL)
tail->_next = qlist->_head;
else
tail->_next = plist->_head;
plist->_head = newnode;
return plist;
}
以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
SList* PartitionPushBack(SList*plist, int x)//以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
{
//采用的是尾插法
assert(plist);
if (plist->_head == NULL)
return plist;
SListNode *big = NULL;
SListNode *big_tail= NULL;
SListNode *small = NULL;
SListNode *small_tail = NULL;
for (SListNode*cur = plist->_head; cur != NULL; cur = cur->_next)
{
if (cur->_data < x)
{
if (big_tail == NULL)
{
big = big_tail = cur;
}
else
{
big_tail->_next = cur;
big_tail = cur;
}
}
else
{
if (small_tail == NULL)
{
small = small_tail = cur;
}
else
{
small_tail->_next = cur;
small_tail = cur;
}
}
}
if (small_tail!= NULL)
{
small_tail->_next = big;
}
if (big_tail != NULL)
{
big_tail->_next = NULL;
}
if (small != NULL)
{
plist->_head = small;
}
else
plist->_head = big;
return plist;
}