Skip to content

Instantly share code, notes, and snippets.

@jeremyabbott
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyabbott/ed18a42aa760d8cf4c47 to your computer and use it in GitHub Desktop.
Save jeremyabbott/ed18a42aa760d8cf4c47 to your computer and use it in GitHub Desktop.
Get Loop and Tail lengths from Linked List
open System.Text.RegularExpressions;
let buildList args =
Regex.Matches(args, "(\d\,\s?\d)")
|> Seq.cast<Match>
|> Seq.map (fun m -> m.Value.Replace(" ", "").Split([|','|]))
|> Seq.toList
let findDuplicatePointer list =
let rec findCycleRec (oldList:list<array<string>>) (newList:list<array<string>>) =
match oldList with
| [] -> None
| x::xs when List.exists (fun (i:array<string>) -> i.[0] = x.[1]) newList -> Some x
| x::xs -> findCycleRec xs (x::newList)
findCycleRec list []
let getListInfo list maybeElement =
let index =
match maybeElement with
| Some (x:array<string>) -> (List.findIndex (fun (i:array<string>) -> i.[1] = x.[1]) list)
| None -> -1
let tailLength = index + 1
let loopLength = (List.length list) - tailLength
(tailLength, loopLength)
let printListInfo listArgs =
let list = buildList listArgs
let element = findDuplicatePointer list
printfn "%A" (getListInfo list element)
[<EntryPoint>]
let main argv =
printListInfo "[1, 2], [2, 3], [3, 4], [4, 2]"
printListInfo "[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 3]"
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment