问题描述
所以我正在解决 leetcode 的问题,我们必须返回尾部连接的节点,但是当我使用 #include <stdio.h>
#include <stdlib.h>
int rows = 2,cols=2 ;
struct Data
{
int longtitude;
};
struct Data* WritingData()
{
struct Data *arr = (struct Data*)malloc(rows* cols* sizeof(struct Data));
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d",&(arr + i*cols+ j)->longtitude);
free(arr);
return(arr);
}
int main()
{
struct Data *arr = WritingData();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{
printf(" %d ",(arr + i*cols+ j)->longtitude);
}
printf("\n");
}
}
时解决方案出错,而当我使用 fast = head->next
时解决方案正确。我明白后者是如何正确的,但我不太明白为什么?
示例: 输入:head = [3,2,-4],pos = 1 输出:tail 连接到节点索引 1
fast = head
正确的代码
ListNode *slow = head,*fast = head->next;
while(fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
break;
}
if(fast == NULL || fast->next == NULL){
return NULL;
}
slow = head;
while(fast != slow){
slow = slow->next;
fast = fast->next;
}
return fast;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)