Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Created September 21, 2020 08:28
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 kulicuu/117d52975029085e1ead8ea2f15b6b52 to your computer and use it in GitHub Desktop.
Save kulicuu/117d52975029085e1ead8ea2f15b6b52 to your computer and use it in GitHub Desktop.
Linked-List stuff in Coffeescript
# credit https://www.programwitherik.com/how-to-create-a-linked-list-and-stack-in-javascript-2/ for the LinkedList implementation,
# I just did the recursive remove_duplicates function.
c = console.log.bind console
LinkedList = ->
@head = null
LinkedList.prototype.push = (val) ->
node =
value: val
next: null
if not @head
@head = node
else
current = @head
while current.next
current = current.next
current.next = node
lili_1 = new LinkedList()
lili_1.push 3
lili_1.push 34
lili_1.push 292
lili_1.push 1
lili_1.push 3
lili_1.push 15
lili_1.push 24
lili_1.push 1
c lili_1.head
remove_duplicates = (lili) ->
acc = []
current = lili.head
acc.push lili.head.value
while current.next
if (acc.indexOf(current.next.value) isnt -1)
if current.next.next
current.next = current.next.next
else
current.next = null
remove_duplicates lili
else
if current.next
current = current.next
else
lili
enumerate = (lili) ->
current = lili.head
while current.next
c current.value
current = current.next
enumerate lili_1
remove_duplicates lili_1
c ' ----------------- '
enumerate lili_1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment