-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist2.py
36 lines (26 loc) · 1.04 KB
/
linkedlist2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''
Linked Lists - Length & Count
Implement Length() to count the number of nodes in a linked list.
length(null) === 0
length(1 -> 2 -> 3 -> null) === 3
Implement Count() to count the occurrences of an integer in a linked list.
count(null, 1) === 0
count(1 -> 2 -> 3 -> null, 1) === 1
count(1 -> 1 -> 1 -> 2 -> 2 -> 2 -> 2 -> 3 -> 3 -> null, 2) === 4
I've decided to bundle these two functions within the same Kata since they are both very similar.
The push()/Push() and buildOneTwoThree()/BuildOneTwoThree() functions do not need to be redefined.
taken from: http://www.codewars.com/kata/linked-lists-length-and-count/train/python
'''
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
def length(node):
return 0 if node is None else 1 + length(node.next)
def count(node, data):
if node is None:
return 0
if node.data == data:
return 1 + count(node.next, data) if not node.next is None else 1
else:
return 0 + count(node.next, data) if not node.next is None else 0