I have single linked list. Assume that the last node of the single linked list not null and point to some arbitrary node within the list instead of NULL (that mean it not '\\0'
). Therefore the linked list looped but it not point to first node. I want to find last node of linked list. Can you suggest me any algorithm to solve this problem ?
I think this is what you require.
Initialize head
to two pointers.
int *temp1 = head, *temp2 = head;
Increment one pointer twice the step than the other pointer. If the linked list has loops the two pointers will meet at some point in the loop. Let's use temp2
to store this meeting point node.
while (temp1 != temp2)
{
temp1 = temp1->next;
temp1 = temp1->next;
temp2 = temp2->next;
}
Iterate a pointer ( temp1
) to circle the loop and count the length of it.
temp1 = temp1->next; // At this point temp1 is the node of the meeting point
loop_length = 0;
while (temp1 != temp2)
{
temp1 = temp1->next;
loop_length++;
}
Initialize a pointer temp1
to head
and advance loop_length
number of steps.
temp1 = head;
while (i < loop_length)
{
temp1 = temp1->next;
i++;
}
Initialize another pointer temp3
to head
and then simultaneously iterate both temp1
and temp3
until they meet. After the loop termination both will meet at the starting point of the loop.
temp3 = head;
while (temp1 != temp3)
{
temp1 = temp1->next;
temp3 = temp3->next;
}
Now you can take another pointer temp4
starting from temp3
and traverse the list until temp4->next
and temp3
meet.
temp4 = temp3;
while (temp4->next != temp3)
{
temp4 = temp4->next;
}
Now temp4
will have the end of the list.
As I understand it, you have something like this, where the integers are just labels for the sake of naming nodes:
1 -> 2 -> 3 -> 4 -> 2
I think you are asking how to detect that node 4 points to something that occurs earlier in the list. I think from your definition, 4 would be the end of the list. Please verify!
In this case, the easiest way to do it is to start a the beginning and step through your list, each time either marking the node as visited (if you can add data for this purpose), or keep a set structure of visited nodes. At each step you can see if the next node is in the visited set. If it is, you've found the last node, if it's not, you put it in the set and continue.
So for these data we get
current next visited Comment
------- ---- ------- -------
1 2 {1}
2 3 {1, 2}
3 4 {1, 2, 3}
4 2 Stop, 2 is in visited, so 4 is the "last" node.
If I understand your question correctly. You have a linked list where the last pointer is pointing back to a previous entry like so
This Next Etc.
1 2 xxxx
2 3 xxxxx
3 4
4 5
6 3
So you need to keep a table of entries you have already visited. When you find a "next" value which is already in your table then the current entry is the real last entry in the list.
ie
do forever
visited[This] = "Y";
move to Next
if visited[Next] == "Y"
exit loop
next;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.