mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 04:46:29 +08:00
36 lines
No EOL
943 B
Zig
36 lines
No EOL
943 B
Zig
// File: ListNode.zig
|
|
// Created Time: 2023-01-07
|
|
// Author: sjinzh (sjinzh@gmail.com)
|
|
|
|
const std = @import("std");
|
|
|
|
// Definition for a singly-linked list node
|
|
// 编译期泛型
|
|
pub fn ListNode(comptime T: type) type {
|
|
return struct {
|
|
const Self = @This();
|
|
|
|
val: T = 0,
|
|
next: ?*Self = null,
|
|
|
|
// Initialize a list node with specific value
|
|
pub fn init(self: *Self, x: i32) void {
|
|
self.val = x;
|
|
self.next = null;
|
|
}
|
|
};
|
|
}
|
|
|
|
// 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;
|
|
} |