第一种是老师大概说了下思路,我就暂停了,自己写出来的,后来听完老师讲后,还是它的简单,我不需要设置很多指针,还是不行呀呀呀
void Circle_K1(LinkList* plist, int k)
{
assert(plist != nullptr);
//int len = Getsize(plist) - k;
ListNode* pre = plist->head;
ListNode* p = plist->head->next;
int n = 0;
while (p != nullptr)
{
++n;
p = p->next;
}
int len = (n - k) % n;
p = plist->head->next;
for (int i = 0; i < len; ++i)
{
pre = pre->next;
p = p->next;
}
ListNode* q = p;
for (int j = 1; j < k; ++j)
{
q = q->next;
}
q->next = plist->head->next;
plist->head->next = p;
pre->next = NULL;
}
void Circle_K(LinkList* plist, int k)
{
assert(plist->head != NULL && plist->head->next != NULL && k >= 1);
int n = 0;
ListNode* p = plist->head;
while (p->next != NULL)
{
n += 1;
p = p->next;
}
int mov = n - k % n;
p->next = plist->head->next;
while (mov--)
{
p = p->next;
}
plist->head->next = p->next;
p->next = NULL;
}