def sum_list_reverse(llist1, llist2): value1, value2 = 0, 0 if llist1.head is None: value1 = 0 if llist2.head is None: value2 = 0 it1 = llist1.head it2 = llist2.head i1 = 0 i2 = 0 while it1 is not None: value1 += it1.data * pow(10, i1) it1 = it1.next i1 += 1 while it2 is not None: value2 += it2.data * pow(10, i2) it2 = it2.next i2 += 1 total_sum = value1 + value2 print(value1, value2) llist = LinkedList() # 以下这步必须先做 # llist.insert(total_sum % 10) digits_reverse_store(llist, total_sum) llist.print()
class Stack(): def __init__(self, size=100): self.size = size self.stack = LinkedList() self.count = 0 def getCount(self): return self.count def isEmpty(self): result = False if self.count == 0: result = True return result def isFull(self): result = False if self.count == self.size: result = True return result def push(self, value): if self.isFull(): raise IndexError("Cannot push, size of stack is full") else: #print(self.stack) self.stack.insertLast(value) self.count = self.count + 1 def pop(self): topVal = self.top() self.count = self.count - 1 self.stack.removeLast() return topVal def top(self): if self.isEmpty(): raise IndexError("Stack is empty") else: topVal = self.stack.peekLast() #topVal = self.stack[self.count - 1] return topVal
class Queue(): def __init__(self, size=100): self.count = 0 self.size = size self.queue = LinkedList() def getCount(self): return self.count def isEmpty(self): result = False if self.count == 0: result = True return result def isFull(self): result = False if self.count == self.size: result = True return result def peek(self): if self.isEmpty(): raise IndexError("Queue is empty") else: frontVal = self.queue.peekFirst() return frontVal def enqueue(self, value): if self.isFull(): raise IndexError("Cannot enqueue, size of queue is full") else: #self.queue[self.count] = value self.queue.insertLast(value) self.count = self.count + 1 def dequeue(self): frontVal = self.queue.removeFirst() self.count = self.count - 1 return frontVal
def sol_follow_up(llist1, llist2): llist = LinkedList() sum_follow_up(llist1.head, llist2.head, llist) # 这个方法的瑕疵在于,这一步的操作其实应该是递归方程里边重复的一个部分,但是由于最后一步没法并入递归,所以只能在 # 外边另外写一遍了 possible_decimal = 0 if llist.head is not None: possible_decimal = llist.head.data // 10 if possible_decimal > 0: llist.head.data = llist.head.data % 10 llist.insert(possible_decimal) # 注意,此时该节点变成 head node 了 llist.print()
def returnKthToLast(head, k): fast = head # for _ in range(1, k): #range(10) is 0~9 range(1,3) = [1,2] # if fast: # fast = fast.next # else: # return None # let fast pointer go k step in advance i = 1 while i <= k: if fast: fast = fast.next i += 1 else: return None slow = head # fast is now k nodes ahead of slow while fast: fast = fast.next slow = slow.next return slow llist = LinkedList() llist.initial_with_string("abcdefg") # g-f-e-d-c-b-a LinkList.print_from_head_node(returnKthToLast(llist.head, 3))
def __init__(self, size=100): self.count = 0 self.size = size self.queue = LinkedList()
def __init__(self, size=100): self.size = size self.stack = LinkedList() self.count = 0
llist.head.data = llist.head.data % 10 digit = (value1 + value2) + possible_decimal llist.insert(digit) # 注意,此时该节点变成 head node 了 def sol_follow_up(llist1, llist2): llist = LinkedList() sum_follow_up(llist1.head, llist2.head, llist) # 这个方法的瑕疵在于,这一步的操作其实应该是递归方程里边重复的一个部分,但是由于最后一步没法并入递归,所以只能在 # 外边另外写一遍了 possible_decimal = 0 if llist.head is not None: possible_decimal = llist.head.data // 10 if possible_decimal > 0: llist.head.data = llist.head.data % 10 llist.insert(possible_decimal) # 注意,此时该节点变成 head node 了 llist.print() l1 = LinkedList() l1.initial_with_list((7, 1, 6)) l2 = LinkedList() l2.initial_with_list((5, 9, 2)) l1.print() l2.print() sol_follow_up(l1, l2)
else: small_tail.next = i small_tail = i else: if big_head is None: big_head = i big_tail = i else: big_tail.next = i big_tail = i i_slow = i i = i.next ## 这一步是最重要的。主要是把一些没用的链接马上砍掉,因为没用的链接容易导致‘环’的出现! # i_slow.next = None ## 这一步是最重要的。主要是把一些没用的链接马上砍掉,因为没用的链接容易导致‘环’的出现! if big_tail is not None: big_tail.next = None if small_tail is not None: if big_head is not None: small_tail.next = big_head return small_head else: return big_head from LinkList import print_from_head_node llist = LinkedList() llist.initial_with_string("abcdefghi") # i-h-g-f-e-d-c-b-a (del = e) print_from_head_node(partition(llist, 'c'))