-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge2SortedList.jl
62 lines (51 loc) · 1.52 KB
/
Merge2SortedList.jl
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
mutable struct ListNode
val::Int
next::Union{ListNode,Nothing}
# Constructor for a single node
ListNode(val::Int) = new(val, nothing)
# Constructor for node with next pointer
ListNode(val::Int, next::Union{ListNode,Nothing}) = new(val, next)
end
function merge_two_lists(
list1::Union{ListNode,Nothing}, list2::Union{ListNode,Nothing}
)::Union{ListNode,Nothing}
isnothing(list1) && isnothing(list2) && return nothing
isnothing(list1) && return list2
isnothing(list2) && return list1
dummy = ListNode(0)
current = dummy
while !isnothing(list1) && !isnothing(list2)
if list1.val <= list2.val
current.next = list1
list1 = list1.next
else
current.next = list2
list2 = list2.next
end
current = current.next
end
# Attach remaining nodes
current.next = isnothing(list1) ? list2 : list1
return dummy.next
end
# Helper function to print the list
function print_list(head::Union{ListNode,Nothing})
current = head
while !isnothing(current)
print(current.val, " -> ")
current = current.next
end
println("nothing")
end
if abspath(PROGRAM_FILE) == @__FILE__
# Create test lists
l1 = ListNode(1, ListNode(2, ListNode(4)))
l2 = ListNode(1, ListNode(3, ListNode(4)))
println("List 1:")
print_list(l1)
println("List 2:")
print_list(l2)
println("Merged list:")
result = merge_two_lists(l1, l2)
print_list(result)
end