0% found this document useful (0 votes)
0 views

recursion2

Uploaded by

krishkdd0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

recursion2

Uploaded by

krishkdd0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

class ListNode:

def __init__(self,value):

self.value = value

self.next = None

class Solution:

def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:

dummy = ListNode()

res = dummy

total = carry = 0

while l1 or l2 or carry:

total = carry

if l1:

total += l1.val

l1 = l1.next

if l2:

total += l2.val

l2 = l2.next

num = total % 10

carry = total // 10

dummy.next = ListNode(num)

dummy = dummy.next

return res.next
class Solution:

def getDecimalValue(self, head: ListNode) -> int:

answer = 0

while head:

answer = 2*answer + head.val

head = head.next

return answer

class Solution:

def fib(self, n: int) -> int:

if n <= 1:

return n

return self.fib(n - 1) + self.fib(n - 2)

class Solution:

def fib(self, n: int) -> int:

if n <= 1:

return n

a, b = 0, 1

for _ in range(2, n + 1):

a, b = b, a + b

return b

class Solution:

def isPowerOfTwo(self, n: int) -> bool:

return n > 0 and (n & (n - 1)) == 0


class Solution:

def isPowerOfThree(self, n: int) -> bool:

if n <= 0:

return False

while n % 3 == 0:

n //= 3

return n == 1

class Solution:

def isPowerOfFour(self, n: int) -> bool:

if n <= 0:

return False

if n == 1:

return True

return n % 4 == 0 and self.isPowerOfFour(n // 4)

You might also like