class Solution(object):
def fibonacci(self, K):
"""
input: int K
return: int
"""
# write your solution here
if K < 0:
return 0
if K == 0 or K == 1:
return K
return self.fibonacci(K-1)+self.fibonacci(K-2)
def getsum(n):
acc=0
for num in xrange (1,n+1):
acc+=num
return acc
def getsum(n):
if n==1:
return 1
return n+getsum(n-1)
class Solution(object):
def power(self, a, b):
"""
input: int a, int b
return: long
"""
# write your solution here
if b==0:
return 1
elif b==1: #可以comment out 因为最后会到1
return a
midres = self.power(a, b//2)
return midres*midres if b%2==0 else midres*midres*a
def PrintSinglyLinkedList(head):
if head is None:
return
print head.value
PrintSinglyLinkedList(head.next)
def search_by_value (head, value):
if head is None:
return None
if head.value==value:
return head
return search_by_value (head.next, value)
def reverse (head): #reverse the linkedlist headed by head
if head is None or head.next is None:
# linked list is empty or contains only one node
return head
node = reverse(head.next)
# 当前head的头节点就是所反转list的尾节点 在执行完head.next这一行之后,head.next的值还是原来的
tail = head.next
tal.next = head
head.next = None
return node