Hone logo
Hone
Problems

Implementing Self-Referencing Structures in TypeScript

Self-referencing structures, like linked lists or trees, are fundamental data structures in computer science. TypeScript, with its strong typing, allows us to elegantly define and work with these structures, ensuring type safety while leveraging the power of recursion. This challenge focuses on implementing a simple self-referencing structure: a singly linked list.

Problem Description

You are tasked with creating a LinkedListNode class in TypeScript that represents a node in a singly linked list. Each node should contain a data property of type number and a next property, which is either another LinkedListNode or null. The goal is to define the class with proper TypeScript typing to ensure type safety when creating and manipulating the linked list. You should also provide a basic function createLinkedList that constructs a linked list from an array of numbers.

Key Requirements:

  • LinkedListNode Class: Define a class named LinkedListNode with the following properties:
    • data: A number representing the data stored in the node.
    • next: A LinkedListNode | null representing the next node in the list. It can be another LinkedListNode or null if it's the last node.
  • Type Safety: Ensure that the TypeScript compiler can verify the correctness of your code, preventing type-related errors.
  • createLinkedList Function: Create a function createLinkedList(arr: number[]): LinkedListNode | null that takes an array of numbers as input and returns the head of a linked list constructed from the array. If the input array is empty, the function should return null.

Expected Behavior:

The createLinkedList function should create a linked list where each element of the input array becomes the data of a node, and the nodes are linked together in the order they appear in the array. The last node's next property should be null.

Edge Cases to Consider:

  • Empty input array: The function should return null.
  • Array with a single element: The function should create a linked list with only one node.

Examples

Example 1:

Input: [1, 2, 3]
Output: { data: 1, next: { data: 2, next: { data: 3, next: null } } }
Explanation: The function creates a linked list with three nodes, each containing the corresponding number from the input array. The last node's `next` is `null`.

Example 2:

Input: []
Output: null
Explanation: The function returns `null` because the input array is empty.

Example 3:

Input: [5]
Output: { data: 5, next: null }
Explanation: The function creates a linked list with a single node containing the number 5. The node's `next` is `null`.

Constraints

  • The input array arr will only contain numbers.
  • The length of the input array arr can be between 0 and 1000 (inclusive).
  • The performance of createLinkedList should be linear with respect to the length of the input array (O(n)).

Notes

  • Think about how to recursively or iteratively build the linked list from the input array.
  • Pay close attention to the type definitions to ensure type safety.
  • The next property of the last node must be null. This is crucial for terminating the list.
  • You are only required to implement the LinkedListNode class and the createLinkedList function. You don't need to implement any other linked list operations (e.g., insertion, deletion).
Loading editor...
typescript