mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 15:36:05 +00:00
add zig codes for Section 'Hash Map' and 'Linear Search'
This commit is contained in:
@@ -18,4 +18,18 @@ pub fn ListNode(comptime T: type) type {
|
||||
self.val = x;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Generate a linked list with a list
|
||||
pub fn listToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, list: std.ArrayList(T)) !?*ListNode(T) {
|
||||
var dum = try mem_allocator.create(ListNode(T));
|
||||
dum.init(0);
|
||||
var head = dum;
|
||||
for (list.items) |val| {
|
||||
var tmp = try mem_allocator.create(ListNode(T));
|
||||
tmp.init(val);
|
||||
head.next = tmp;
|
||||
head = head.next.?;
|
||||
}
|
||||
return dum.next;
|
||||
}
|
||||
Reference in New Issue
Block a user