-
Notifications
You must be signed in to change notification settings - Fork 0
/
palidrome_linked_list.ex
39 lines (34 loc) · 1.17 KB
/
palidrome_linked_list.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
defmodule PalindromeLinkedList do
@moduledoc """
Intuition/Approach
At first I was contemplating using a Enum.reduce() but then realized I
could use [head | tail] to traverse the list and store the values in acc.
Then it was simply a comparison to Enum.reverse() to see if it
matched and therefore was a palindrome.
<- Code ->
# Definition for singly-linked list.
#
# defmodule ListNode do
# @type t :: %__MODULE__{
# val: integer,
# next: ListNode.t() | nil
# }
# defstruct val: 0, next: nil
# end
Visible here: https://leetcode.com/problems/palindrome-linked-list/solutions/3693877/elixir-simple-recursion-solution/
"""
@doc """
Function to traverse a struct and store it's value in a list then check
it in reverse and see if it's a palindrome
"""
@spec is_palindrome(head :: ListNode.t | nil) :: boolean
def is_palindrome(head) do
val_list = traverse([head.val | head.next], [])
val_list == Enum.reverse(val_list)
end
defp traverse([], acc), do: acc
defp traverse([val | nil], acc), do: [val] ++ acc
defp traverse([val | node], acc) do
traverse([node.val | node.next], [val] ++ acc)
end
end