I am looking for an algorithm to create a linked list from a set of nodes. For example, let’s assume a node is an airline ticket from a source point to destination. (e.g., Chicago to Detroit) and there are several airline tickets. Assuming all these airline tickets are jumbled, what is the best way to determine the entire journey path.
If there are 5 airline tickets like Chicago->Detroit, Denver->Chicago, Detroit->DC, DC->New York, San Jose->Denver, the algorithm should be able to come up with the correct start to end path.
San Jose -> Denver -> Chicago -> Detroit -> DC -> New York
If you are guaranteed that the nodes will form a single path (ie, maximum indegree is 1, maximum outdegree is 1, exactly one node has indegree 0, exactly one node has outdegree 0), then you could do the following:
in_citiesandout_citiesA -> B, add it toin_citieswith keyAandout_citieswith keyBScurrpoint toS. While the source city ofcurris inout_cities, insert the corresponding node in the beginning of the path. Updatecurrto point to this node.currpoint toS. While the destination city ofcurris inin_cities, add the corresponding node to the end of the path. Updatecurrto point to this node.Now you are done, in linear time with respect to the total number of cities.