Convert a linked list to a string
Description:
Convert a linked list to a string
Related Kata
Although this Kata is not part of an official Series, you may also want to try out Parse a linked list from a string if you enjoyed this Kata.
Preloaded
Preloaded for you is a class, struct or derived data type Node
( depending on the language ) used to construct linked lists in this Kata:
class Node {
public $data, $next;
public function __construct($data, $next = NULL) {
$this->data = $data;
$this->next = $next;
}
}
Prerequisites
This Kata assumes that you are already familiar with the idea of a linked list. If you do not know what that is, you may want to read this article on Wikipedia. Specifically, the linked lists this Kata is referring to are singly linked lists, where the value of a specific node is stored in its data
/ $data
/ Data
property, the reference to the next node is stored in its next
/ $next
/ Next
/ next_node
property and the terminator for a list is null
/ NULL
/ None
/ nil
/ nullptr
/ null()
.
Task
Create a function stringify
which accepts an argument list
/ $list
and returns a string representation of the list. The string representation of the list starts with the value of the current Node
, specified by its data
/ $data
/ Data
property, followed by a whitespace character, an arrow and another whitespace character (" -> "
), followed by the rest of the list. The end of the string representation of a list must always end with null
/ NULL
/ None
/ nil
/ nullptr
/ null()
( all caps or all lowercase depending on the language you are undertaking this Kata in ). For example, given the following list:
new Node(1, new Node(2, new Node(3)))
... its string representation would be:
"1 -> 2 -> 3 -> NULL"
And given the following linked list:
... its string representation would be:
"0 -> 1 -> 4 -> 9 -> 16 -> NULL"
Note that null
/ NULL
/ None
/ nil
/ nullptr
/ null()
itself is also considered a valid linked list. In that case, its string representation would simply be "null"
/ "NULL"
/ "None"
/ "nil"
/ "nullptr"
/ @"NULL"
/ "null()"
( again, depending on the language ).
For the simplicity of this Kata, you may assume that any Node
in this Kata may only contain non-negative integer values. For example, you will not encounter a Node
whose data
/ $data
/ Data
property is "Hello World"
.
Enjoy, and don't forget to check out my other Kata Series :D
Similar Kata:
Stats:
Created | Nov 16, 2016 |
Published | Nov 16, 2016 |
Warriors Trained | 16965 |
Total Skips | 479 |
Total Code Submissions | 27176 |
Total Times Completed | 8854 |
PHP Completions | 245 |
JavaScript Completions | 3505 |
C# Completions | 637 |
Python Completions | 1928 |
Java Completions | 1460 |
Ruby Completions | 176 |
C++ Completions | 739 |
Haskell Completions | 196 |
Objective-C Completions | 27 |
C Completions | 284 |
Fortran Completions | 9 |
NASM Completions | 10 |
Factor Completions | 11 |
COBOL Completions | 3 |
Total Stars | 258 |
% of votes with a positive feedback rating | 92% of 1151 |
Total "Very Satisfied" Votes | 994 |
Total "Somewhat Satisfied" Votes | 129 |
Total "Not Satisfied" Votes | 28 |
Total Rank Assessments | 8 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 7 kyu |
Lowest Assessed Rank | 8 kyu |