# 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](https://www.codewars.com/kata/582c5382f000e535100001a7) 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:

```python
class Node():
    def __init__(self, data, next = None):
        self.data = data
        self.next = next
```

~~~if:objc
*NOTE: In Objective-C, the* `Node` *struct is placed on top of your main solution because there is a "double-import" bug in the Preloaded section at the time of writing (which cannot be fixed on my end).  Attempts to modify it (e.g. to cheat the tests in some way) will likely result in a test crash so it is not recommended for you to modify that section ;)*
~~~

~~~if:c
*NOTE: In C, the* `Node` *struct is placed on top of your main solution (and the [Sample] Test Cases) because the compiler complains about not recognizing the* `Node` *datatype even after adding it to the Preloaded section.  Attempts to modify it (e.g. to cheat the tests in some way) will likely result in a test crash so it is not recommended for you to modify that section ;)*
~~~

If you are attempting this Kata in NASM then note that the code example shown directly above may not be relevant; please refer to the Sample Tests (written in C) for the exact definition of the `Node` structure.

## 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 up on [this article on Wikipedia](https://en.wikipedia.org/wiki/Linked_list).  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

*If you are attempting this Kata in NASM, the code examples shown below may not be relevant at all - please refer to the Sample Tests (written in C) for the exact requirements.*

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:

```python
Node(1, Node(2, Node(3)))
```

... its string representation would be:

```python
"1 -> 2 -> 3 -> None"
```

And given the following linked list:

```python
Node(0, Node(1, Node(4, Node(9, Node(16)))))
```

... its string representation would be:

```python
"0 -> 1 -> 4 -> 9 -> 16 -> None"
```

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

~~~if:fortran
*NOTE: In Fortran, your returned string is* **not** *permitted to contain any leading and/or trailing whitespace.*
~~~

Source: codewars | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: def stringify(node):
	

Solutions:
def stringify(list):
	return 'None' if list == None else str(list.data) + ' -> ' + stringify(list.next)


def stringify(ll):
	r = []
	while ll:
		(r, ll) = (r + [str(ll.data)], ll.next)
	return ' -> '.join(r + ['None'])


def stringify(head):
	if head is None:
		return 'None'
	return '{} -> {}'.format(head.data, stringify(head.__next__))


def stringify(list):
	return str(list.data) + ' -> ' + stringify(list.next) if list is not None else 'None'


class Node:

	def __init__(self, data, next=None):
		self.data = data
		self.next = next

def getValue(self):
	return self.data

def getNext(self):
	return self.__next__

def stringify(node):
	if node == None:
		return 'None'
	return str(getValue(node)) + ' -> ' + stringify(getNext(node))


class Node:

	def __init__(self, data, next=None):
		self.data = data
		self.next = next

def stringify(node):
	k = ''
	while True:
		if node == None:
			k += 'None'
			return k
		else:
			k += '{} -> '.format(node.data)
			node = node.next


def stringify(list_):
	try:
		return '{} -> {}'.format(list_.data, stringify(list_.next))
	except AttributeError:
		return 'None'


def stringify(node):
	return 'None' if not node else f'{str(node.data)} -> ' + stringify(node.__next__)


def stringify(node):
	if not node:
		return 'None'
	return '{} -> {}'.format(node.data, stringify(node.next))


def stringify(node):
	result = ''
	while node:
		result += str(node.data) + ' -> '
		node = node.next
	return result + 'None'


def stringify(node):
	if node == None:
		return 'None'
	return str(node.data) + ' -> ' + stringify(node.next)


def stringify(node):
	if not node:
		return 'None'
	if node.__next__:
		return f'{node.data} -> {stringify(node.next)}'
	else:
		return f'{node.data} -> None'


stringify = lambda n: 'None' if n == None else (lambda s: ([i for i in iter(lambda : ((s['r'].append(str(s['n'].data)), [None for s['n'] in [s['n'].next]]), s['n'] != None)[1], False)], s['r'].append('None'), ' -> '.join(s['r']))[-1])({'r': [], 'n': n})


def stringify(node):
	my_str = ''
	while node:
		my_str += str(node.data) + ' -> '
		node = node.next
	my_str += 'None'
	return my_str


def stringify(node):
	r = []
	while node != None:
		node = (r.append(str(node.data)), node.next)[1]
	return ' -> '.join(r + ['None'])


def recursive(node, string):
	if node is None:
		string += 'None'
		return string
	string += str(node.data) + ' -> '
	node = node.next
	return recursive(node, string)

def stringify(node):
	string = ''
	ans = recursive(node, string)
	return ans


def stringify(node):
	if node is None:
		return 'None'
	data = node.data
	list = []
	current_node = node
	while current_node is not None:
		list.append(str(current_node.data))
		current_node = current_node.__next__
	if current_node is None:
		list.append('None')
	return ' -> '.join(list)


def stringify(node):
	if node is None:
		return 'None'
	itr = node
	llstr = ''
	while itr:
		if itr.next:
			llstr += str(itr.data) + ' -> '
		else:
			llstr += str(itr.data) + ' -> None'
		itr = itr.next
	return llstr


def stringify(node):
	r = ''
	while node != None:
		r += f'{node.data} -> '
		node = node.next
	return r + 'None'


def stringify(node):

	def get(node, L=[]):
		while node:
			(L, node) = (L + [str(node.data)], node.next)
		return L
	return ' -> '.join(get(node) + ['None'])


def stringify(node):
	if node is None:
		return 'None'
	n = node
	l = []
	while True:
		l.append(str(n.data))
		if n.next is None:
			break
		else:
			n = n.next
	l.append('None')
	return ' -> '.join(l)


def stringify(node):
	try:
		emptylist = []
		while type(node.data) is int:
			emptylist.append(str(node.data))
			node = node.next
	except:
		emptylist.append('None')
		return ' -> '.join(emptylist)


def stringify(node, output_array=None):
	if output_array is None:
		output_array = []
	if not node:
		output_array.append('None')
	while node:
		output_array.append(node.data)
		return stringify(node.__next__, output_array)
	return ' -> '.join((str(i) for i in output_array))


def stringify(node):
	return ''.join(str(node.data) + ' -> ' + stringify(node.next)) if node else 'None'


def stringify(node):
	final = ''
	try:
		final += str(node.data) + ' -> '
		while node.next:
			final += str(node.next.data)
			final += ' -> '
			node = node.next
	except AttributeError:
		pass
	final += 'None'
	return final


def stringify(node):
	new = []
	node = node
	while node is not None:
		new.append(str(node.data))
		node = node.next
	else:
		new.append('None')
	return ' -> '.join(new)


class Node:

	def __init__(self, data, next=None):
		self.data = data
		self.next = next

def stringify(list):
	if list == None:
		return 'None'
	else:
		return str(list.data) + ' -> ' + stringify(list.next)


def stringify(node):
	string = ''
	while node:
		string += str(node.data)
		string += ' -> '
		node = node.next
	string += 'None'
	return string


def stringify(node):
	return f'{node.data} -> {stringify(node.next)}' if node else str(None)


def stringify(node):
	a = ''
	if node == None:
		return 'None'
	else:
		node_data = str(node.data)
		return node_data + ' -> ' + stringify(node.__next__)


def stringify(node):
	if node == None:
		return 'None'
	else:
		s = str(node.data) + ' -> '
		return s + stringify(node.next)


def stringify(node):
	if node == None:
		return 'None'
	return '{} -> {}'.format(node.data, stringify(node.__next__))


def stringify(node):
	s = []
	while node:
		s.append(node.data)
		node = node.next
	s.append(None)
	return ' -> '.join(list(map(str, s)))


def stringify(node):
	s = ''
	while node:
		s += f'{node.data} -> '
		node = node.next
	s += 'None'
	return s


def stringify(node):
	res = ''
	while node:
		res += '{} -> '.format(str(node.data))
		node = node.next
	return res + 'None'


def stringify(node):
	x = []
	while True:
		try:
			x.append(str(node.data))
			node = node.next
		except:
			break
	x.append('None')
	return ' -> '.join(x)


def stringify(node):
	return 'None' if node is None else f'{node.data} -> {stringify(node.next)}'


def stringify(node):
	res = []
	curr = node
	while curr:
		res.append(str(curr.data))
		curr = curr.next
	res.append('None')
	return ' -> '.join(res)


def stringify(node):
	list = []
	f = node
	while f is not None:
		list.append(str(f.data))
		f = f.next
	list.append('None')
	return ' -> '.join(list)


def stringify(node):
	string_list = ''
	current = node
	while current:
		string_list += str(current.data) + ' -> '
		current = current.next
	string_list += 'None'
	return string_list


def stringify(node):
	if node == None:
		return 'None'
	lst = []
	n = node
	while n.__next__ != None:
		lst.append(str(n.data))
		n = n.__next__
	lst.append(str(n.data))
	lst.append('None')
	return ' -> '.join(lst)


def stringify(node):
	node_str = ''
	while node:
		arrow = ' -> '
		node_str += str(node.data) + arrow
		node = node.__next__
	return node_str + 'None'


def stringify(node):
	lst = []
	while node:
		lst += [str(node.data)]
		lst += [' -> ']
		node = node.next
	lst += ['None']
	return ''.join(lst)


def stringify(node):
	if node == None:
		return 'None'
	i = node
	t = ''
	while i.next != None:
		t += str(i.data) + ' -> '
		i = i.next
	t += str(i.data) + ' -> None'
	return t


def stringify(node):
	a = []
	if not node:
		return 'None'
	while node:
		a.append(str(node.data))
		node = node.__next__
	a.append('None')
	print(a)
	return ' -> '.join(a)


def stringify(node):
	ans = []
	while node:
		ans.append(str(node.data))
		node = node.__next__
	ans.append('None')
	return ' -> '.join(ans)


def stringify(node):
	if node is None:
		return 'None'
	result = ''
	while node.__next__ is not None:
		result += str(node.data)
		result += ' -> '
		node = node.__next__
	result += str(node.data)
	result += ' -> None'
	return result


def stringify(node):
	if not node:
		return 'None'
	list = [str(node.data)]
	curr = node
	while curr != None:
		curr = curr.next
		list.append(str(curr.data) if curr else 'None')
	return ' -> '.join(list)


def stringify(node):
	n = 'None'
	if node:
		(n, r) = (node.__next__, f'{node.data} -> ')
		while n:
			r += f'{n.data} -> '
			n = n.__next__
		r += 'None'
	else:
		r = n
	return r


def stringify(node):
	if node is not None:
		return str(node.data) + ' -> ' + stringify(node.next)
	else:
		return 'None'


def stringify(node):
	if node == None:
		return 'None'
	elif node.next == None:
		return f'{node.data} -> None'
	else:
		return f'{node.data} -> {stringify(node.next)}'


def stringify(node):
	if not node:
		return 'None'
	ls = []
	while node.next != None:
		ls.append(str(node.data))
		node = node.next
	ls.append(str(node.data) + ' -> None')
	return ' -> '.join(ls)


def stringify(node):
	return 'None' if node is None else '%d -> %s' % (node.data, stringify(node.__next__))


def stringify(node):
	if node == None:
		return 'None'
	ret = ''
	while node.data != None:
		ret += str(node.data) + ' -> '
		if not node.__next__:
			ret += 'None'
			break
		node = node.__next__
	return ret


def stringify(node):
	if node is None:
		return 'None'
	res = []
	while True:
		if node.__next__ is None:
			res.append(str(node.data))
			res.append('None')
			break
		res.append(str(node.data))
		node = node.__next__
	return ' -> '.join(res)


def stringify(node):
	if node == None:
		return 'None'
	return f'{node.data} -> {stringify(node.next)}'


def stringify(node):
	elems = []
	cur_node = node
	while cur_node != None:
		elems.append(cur_node.data)
		cur_node = cur_node.next
	if elems == []:
		return 'None'
	return ' -> '.join((str(x) for x in elems)) + ' -> ' + 'None'


def stringify(node):
	if not node:
		return 'None'
	string = node.data
	node = node.next
	while node:
		string = f'{string} -> {node.data}'
		node = node.next
	return f'{string} -> {node}'


def stringify(node):
	List = []
	while node and node.data != None:
		List.append(str(node.data))
		node = node.next
	List.append('None')
	return ' -> '.join(List)


def stringify(node):
	currentNode = node
	res = ''
	while currentNode is not None:
		res += f'{currentNode.data} -> '
		currentNode = currentNode.next
	if currentNode is None:
		res += 'None'
	return res


def stringify(node):
	result = ''
	while node != None:
		result += f'{node.data} -> '
		node = node.next
	result += 'None'
	return result


def stringify(node):
	if node == None:
		return 'None'
	returnString = ''
	while node != None:
		returnString += str(node.data) + ' -> '
		node = node.next
	returnString += 'None'
	return returnString


def stringify(node):
	head = node
	curr = head
	strList = []
	while curr != None:
		strList.append(curr.data)
		curr = curr.next
	strList.append(None)
	return ' -> '.join((str(x) for x in strList))


def stringify(node):
	if node is None:
		return 'None'
	output = str(node.data) + ' -> '
	while node.next != None:
		node = node.next
		output += str(node.data) + ' -> '
	output += 'None'
	return output


def stringify(node):
	ret = ''
	while node:
		ret += str(node.data) + ' -> '
		node = node.next
	return ret + 'None'


def stringify(node):
	curr = node
	res = []
	while curr != None:
		res.append(curr.data)
		curr = curr.next
	res.append(None)
	return ' -> '.join((str(x) for x in res))


def stringify(node):
	(seq, current) = ([], node)
	while current:
		seq.append(str(current.data))
		current = current.next
	seq.append('None')
	return ' -> '.join(seq)


def stringify(node):
	if not node:
		return 'None'
	output = ''
	while node:
		output += str(node.data) + ' -> '
		node = node.next
	output += 'None'
	return output


def stringify(node):
	if not node:
		return 'None'
	return ' -> '.join([x for x in rec_stringify(node)])

def rec_stringify(node):
	if not node.__next__:
		return [str(node.data), 'None']
	else:
		return [str(node.data)] + rec_stringify(node.__next__)


def stringify(node):
	st = ''
	while node:
		st = st + str(node.data) + ' -> '
		node = node.__next__
	st = st + 'None'
	return st


def stringify(node):
	string = ''
	if node == None:
		return 'None'
	else:
		n = node.next
		string = string + str(node.data) + ' -> ' + stringify(node.next)
		return string


def stringify(node):
	if node == None:
		return 'None'
	to_remove = '(),'
	table = {ord(char): None for char in to_remove}
	head = int(node.data)
	tail = node.__next__
	if tail == None:
		return str(' -> '.join((str(head), 'None')).translate(table))
	else:
		return str(' -> '.join((str(head), stringify(tail))).translate(table))


def stringify(node):
	if node == None:
		return 'None'
	n = node
	r = ''
	while n.next != None:
		r = r + str(n.data) + ' -> '
		n = n.next
	return r + str(n.data) + ' -> None'


def stringify(node):
	count = 0
	next_node = ''
	while count < 100:
		try:
			current_node = eval('node.' + count * 'next.' + 'data')
			next_node += str(current_node) + ' -> '
			count += 1
		except:
			return 'None' if node == None else next_node + 'None'


def stringify(node):
	ret = ''
	try:
		while node.data != None:
			ret += str(node.data) + ' -> '
			node = node.next
	except:
		ret += 'None'
	return ret


stringify = lambda node, s='': stringify(node.next, s + f'{node.data} -> ') if node else s + 'None'


def stringify(node):
	res = ''
	while not node == None:
		res += str(node.data) + ' -> '
		node = node.next
	res += 'None'
	return res


def stringify(node):
	to_print = ''
	while node != None:
		to_print += str(node.data) + ' -> '
		node = node.__next__
	return to_print + 'None'


def stringify(node):
	s = ''
	while node:
		s += str(node.data)
		node = node.next
		s += ' -> '
	return s + 'None'


def stringify(node):
	if not node:
		return 'None'
	lst = [str(node.data)]
	next_node = node.next
	while next_node != None:
		lst.append(str(next_node.data))
		next_node = next_node.next
	return ' -> '.join(lst) + ' -> None'