Nodes and Conversions

Step 1: Nodes and Conversions

A singly linked list is a chain of nodes. Each node holds a value and a pointer to the next node. The last node points to None.

The Node class is already written for you. Your job is to implement two helpers that make the rest of this challenge readable:

  • from_list(values): build a linked list from a Python list and return the head node. Preserve order, so the first value becomes the head. An empty list should return None.
  • to_list(head): walk the chain following each next pointer and return a plain Python list of the values. A head of None should return an empty list.

These two helpers let every later test build a list and inspect it without touching raw pointers.

Hints
Starter code
class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next


def from_list(values):
    """Build a singly linked list from values and return the head node."""
    # TODO: create Node objects linked in order and return the head (None if empty)
    pass


def to_list(head):
    """Convert a linked list back into a Python list of values."""
    # TODO: follow the next pointers, collecting each value into a list
    pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.