All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Lost my pointer 2 (Posted on 2003-12-18) Difficulty: 3 of 5
You are given a pointer to the head of a SINGLY linked list. Write a function that takes as an argument one pointer, and determines whether or not the list is "looped".

A "looped" list means that the linked list has a node that points to a PREVIOUS member (thereby creating a cycle).

Clearly, if a list is looped, and you attempt to simply go from node to each next node until you reach the end, you will never get to an end (just going around and around the loop forever).

Conditions: The list (and the loop, if it exists) may be of any given size (up to available memory), and your function has only 64 bytes (16 4-byte pointers) of available heap/stack space).

***Extra Credit: What is the speed (big-O notation) of your solution?

For those of you non-computer types: a singly linked list is simply a list of records where each record (node) points to the NEXT record. (The only way to get to a node is to first be at the previous node.) The first node is special in that we keep track of where it is.

So, we can always get to the first node, but to get to the second we must first get to the first node. To get to the third, we must first get to the second, and so on.

Therefore, it is an easy matter of traversing all the nodes in order. But it is impossible to go backwards (unless one keeps track of all previously visited nodes) or to access the nodes in any other order (like random access or alphabetically).

Normally, the last node points to NULL, and we generally know to stop at that point.

This problem arises when one of the nodes MIGHT point to a node that occurred in the list previously. (For instance, the 651st node points to the 83rd node.) Then we will end up going from the 651st -> 83rd -> 84th... and continue looping.

The question is... how do we know that we've looped? (And how quickly can we determine whether or not we looped.)

See The Solution Submitted by SilverKnight    
Rating: 4.0000 (2 votes)

Comments: ( You must be logged in to post comments.)
  Subject Author Date
Solutioneasy enoughRobert2004-06-06 02:06:21
re(2): No SubjectGuy Soffer2004-04-07 04:39:13
re: No SubjectThalamus2004-04-05 14:33:24
No SubjectGuy Soffer2004-04-05 06:20:11
re: SolutionThalamus2004-03-17 07:27:37
SolutionSolutionprabhakhar2004-03-17 06:36:44
Hints/TipsNo Subjectprabhakhar2004-03-17 06:34:14
Some ThoughtsIdea for solutionphenomenon2004-02-21 03:00:43
solutiontom2003-12-19 23:48:44
re(5): Racing around -- simulationBrian Wainscott2003-12-19 18:51:12
re(4): Racing around -- simulationBrian Wainscott2003-12-19 18:45:38
GJ silver knightdrew2003-12-19 14:16:29
re(3): Racing aroundFederico Kereki2003-12-19 11:50:31
Questionre(2): Racing aroundCharlie2003-12-18 22:26:40
Solutionre: Racing aroundFederico Kereki2003-12-18 21:12:07
re: Solution - indention solutionSilverKnight2003-12-18 16:15:20
SolutionSolutionBrian Wainscott2003-12-18 16:03:52
re: I don't understand the definition of aCharlie2003-12-18 15:55:10
Solutiona solutionCharlie2003-12-18 15:45:49
QuestionI don't understand the definition of a "looped" list.Penny2003-12-18 15:41:48
SolutionRacing arounde.g.2003-12-18 15:04:44
Hints/TipsFull SolutionSilverKnight2003-12-18 14:09:01
Please log in:
Remember me:
Sign up! | Forgot password

Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Copyright © 2002 - 2019 by Animus Pactum Consulting. All rights reserved. Privacy Information