c - 如果链接列表中存在循环,则删除循环
问题描述
我正在尝试编写可以从链表中删除循环的代码,如果列表中有的话。
Input:
N = 3 //total number of nodes in the list
value[] = {1,3,4}
C = 2 //position of the last connected node, if it is zero it is not connected
Output: 1
void removeLoopLlinkedList(Node* head)
{
if(!head) return;
Node* first= head->next;
Node* last= head;
while( first!= last)
{
if( !first|| !last->next ) return;
first=first->next->next;
last=last->next;
}
int size = 1;
first = first->next;
while( first== last)
{
size++;
first= first->next;
}
first= head;
for(int i=0; i<size-1; i++)
first= first->next;
while( last->next != last )
{
first= first->next;
last= last->next;
}
}
我找不到我的错误,也没有得到想要的输出。我怎么解决这个问题?
先感谢您!
解决方案
推荐阅读
- java - 无法在 Windows 中使用 java VisualVM 连接到包含 java 1.8.0 的远程 ubuntu 机器
- spring-boot - 使用 Springboot webclient 反序列化 OffsetDateTime
- c# - 提取特定标题下的文本或仅提取 listItems
- flutter - 如何在颤动中绘制半个图标
- python - 我需要将 python 脚本与库集成到 spring-boot 应用程序
- flutter - 异步任务不在 WorkManagers CallbackDispatcher 中工作
- android - AndroidViewModel 按需取消作业
- swiftui - NavigationView SwiftUI 中的 NavigationView
- python - 使用命名参数调用方法会导致“参数的多个值”错误
- python - 如何识别该数据框在熊猫中有重复的列名?