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:
These two helpers let every later test build a list and inspect it without touching raw pointers.
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
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
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:
These two helpers let every later test build a list and inspect it without touching raw pointers.
Press Run Tests or ββ© to check your solution.