diff --git a/invert-binary-tree/han.exs b/invert-binary-tree/han.exs new file mode 100644 index 000000000..fda55d953 --- /dev/null +++ b/invert-binary-tree/han.exs @@ -0,0 +1,37 @@ +# Definition for a binary tree node. +# +# defmodule TreeNode do +# @type t :: %__MODULE__{ +# val: integer, +# left: TreeNode.t() | nil, +# right: TreeNode.t() | nil +# } +# defstruct val: 0, left: nil, right: nil +# end + +defmodule Solution do + @spec invert_tree(root :: TreeNode.t | nil) :: TreeNode.t | nil + def invert_tree(nil), do: nil + + def invert_tree(root) do + invert(root, root.left, root.right) + end + + defp invert(parent, nil, nil), do: parent + + defp invert(parent, left, nil) do + %TreeNode{parent | left: nil, right: invert(left, left.left, left.right)} + end + + defp invert(parent, nil, right) do + %TreeNode{parent | left: invert(right, right.left, right.right), right: nil} + end + + defp invert(parent, left, right) do + tmp = left + + %TreeNode{parent | + left: invert(right, right.left, right.right), + right: invert(tmp, tmp.left, tmp.right)} + end +end diff --git a/linked-list-cycle/han.rb b/linked-list-cycle/han.rb new file mode 100644 index 000000000..6a4ba59c1 --- /dev/null +++ b/linked-list-cycle/han.rb @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val) +# @val = val +# @next = nil +# end +# end + +# @param {ListNode} head +# @return {Boolean} +def hasCycle(head) + return false unless head && head.next + + slow = head + fast = head.next + + loop do + break true if slow == fast + + slow = slow.next + fast = fast&.next&.next + + break false unless fast + end + end diff --git a/merge-two-sorted-lists/han.exs b/merge-two-sorted-lists/han.exs new file mode 100644 index 000000000..b5c8254ba --- /dev/null +++ b/merge-two-sorted-lists/han.exs @@ -0,0 +1,24 @@ +# Definition for singly-linked list. +# +# defmodule ListNode do +# @type t :: %__MODULE__{ +# val: integer, +# next: ListNode.t() | nil +# } +# defstruct val: 0, next: nil +# end + +defmodule Solution do + @spec merge_two_lists(list1 :: ListNode.t | nil, list2 :: ListNode.t | nil) :: ListNode.t | nil + def merge_two_lists(nil, nil), do: nil + def merge_two_lists(nil, list2), do: list2 + def merge_two_lists(list1, nil), do: list1 + + def merge_two_lists(list1, list2) when list1.val >= list2.val do + %ListNode{list2 | next: merge_two_lists(list1, list2.next)} + end + + def merge_two_lists(list1, list2) do + %ListNode{list1 | next: merge_two_lists(list1.next, list2)} + end +end diff --git a/reverse-linked-list/han.exs b/reverse-linked-list/han.exs new file mode 100644 index 000000000..c7e1aa1e1 --- /dev/null +++ b/reverse-linked-list/han.exs @@ -0,0 +1,25 @@ +# Definition for singly-linked list. +# +# defmodule ListNode do +# @type t :: %__MODULE__{ +# val: integer, +# next: ListNode.t() | nil +# } +# defstruct val: 0, next: nil +# end + +defmodule Solution do + def reverse_list(head) when is_nil(head), do: nil + + @spec reverse_list(head :: ListNode.t | nil) :: ListNode.t | nil + def reverse_list(head, arr \\ []) do + new_arr = List.insert_at(arr, -1, head.val) + if head.next do + reverse_list(head.next, new_arr) + else + Enum.reduce(tl(new_arr), %ListNode{val: hd(new_arr), next: nil}, fn x, acc -> + %ListNode{val: x, next: acc} + end) + end + end +end diff --git a/valid-parentheses/han.exs b/valid-parentheses/han.exs new file mode 100644 index 000000000..4d2f425cb --- /dev/null +++ b/valid-parentheses/han.exs @@ -0,0 +1,21 @@ +defmodule Solution do + def is_valid(s) when byte_size(s) > 1 do + brackets = %{")" => "(", "}" => "{", "]" => "["} + opens = ["[", "{", "("] + + result = Enum.reduce_while(String.graphemes(s), [], fn x, acc -> + cond do + x in opens -> + {:cont, List.insert_at(acc, -1, x)} + brackets[x] == List.last(acc) -> + {:cont, List.delete_at(acc, -1)} + true -> + {:halt, List.insert_at(acc, -1, x)} + end + end) + + length(result) < 1 + end + + def is_valid(_), do: false +end