def create_lists_1(r): q=deque([]) level=deque([]) lists=[] q.append(r) level.append(0) level_list=LinkList() level_list.append_node(r) while q: current_node=q.popleft() current_level=level.popleft() if current_node.left: q.append(current_node.left) level.append(current_level+1) if current_node.right: q.append(current_node.right) level.append(current_level+1) if q: # if this node is at the same level of the previous node, then add this node to the list if level[0]==current_level: level_list.append_node(q[0]) # else, then pack the list to lists, create a new list, and add this node to the list else: lists.append(level_list) level_list=LinkList() level_list.append_node(q[0]) # if it's the last element, pack to lists else: lists.append(level_list) return lists
def __init__(self, n): #use `self` to attach variable to this player #otherwise its like a static variable #every inst of player points to the same variable/memory self.name = n self.hand = LinkList() self.hiddenStack = LinkList() self.shownStack = LinkList() self.hasTurn = False
def create_lists_2(r): lists=[] current=LinkList() if r: current.append_node(r) while len(current)>0: lists.append(current) # pack to lists parents=current # go one level down current=LinkList() # create a new list for parent in parents: if parent.left: current.append_node(parent.left) if parent.right: current.append_node(parent.right) return lists
class Deck: suits = ['S', 'D', 'C', 'H'] numbers = [ 'a', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k' ] deck = LinkList() def __init__(self): ##new deck order for a in self.suits: for b in self.numbers: self.deck.add(b + a) #self.deck.append(b+a) def shuffle(self): for i in range(0, 51): random = randint(i + 1, 51) self.deck.swap(i, random) def swap(self, a, b): temp = self.deck[a] self.deck[a] = self.deck[b] self.deck[b] = temp def show(self): self.deck.show() def draw(self): return self.deck.removeHead()
def __init__(self): self.eliminatedPlayers = [] ##check if eliminated, check if p1.hasTurn self.removedCards = [] self.currentStack = LinkList() self.players = [] #arranged by card rank self.deck = None self.currTurn = 0 self.sevenflag = None #high or low
def test_link_list(self): lst = LinkList() a = (3, 35, -5, 7, 10) lst.create(a) lst.head.print() p = lst.head for b in a: assert b == p.val p = p.next new = 55 lst.insert(1, new) p = lst.head p = p.next assert new == p.val
def create_lists_3_helper(r, lists, level): if not r: return # if the level is not visited before, create a new list if level>len(lists)-1: current_list=LinkList() current_list.append_node(r) lists.append(current_list) # else, get the list, append the tree node else: current_list=lists[level] current_list.append_node(r) create_lists_3_helper(r.left, lists, level+1) create_lists_3_helper(r.right, lists, level+1)
def __init__(self, max_size): self.table = [LinkList() for i in range(max_size)] self.size = 0
from LinkList import LinkList, ListNode list1 = LinkList() list1.append(ListNode(1)) list1.append(ListNode(2)) list1.append(ListNode(3)) list1.append(ListNode(3)) list1.append(ListNode(4)) list1.append(ListNode(4)) list1.append(ListNode(5)) list1.append(ListNode(6)) list1.append(ListNode(7)) list1.append(ListNode(7)) list1.append(ListNode(8)) list1.append(ListNode(9)) list1.append(ListNode(9)) list1.append(ListNode(9)) def removeDuplicate(head): lastVal = None current = head.next while current: nextNode = current.next if current.val == lastVal: current.next = nextNode.next else: lastVal = current.val current = nextNode return head
#-*-coding:utf-8 -*- # 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。 from LinkList import LinkList, ListNode list1 = LinkList() list2 = LinkList() list1.append(ListNode(1)) list1.append(ListNode(3)) list1.append(ListNode(5)) list1.append(ListNode(7)) list1.append(ListNode(9)) list2.append(ListNode(2)) list2.append(ListNode(4)) list2.append(ListNode(6)) list2.append(ListNode(8)) list2.append(ListNode(10)) def mergeLinkList(list1, list2): head = list1.head current = head current1 = list1.head.next current2 = list2.head.next while current1 and current2: if current1.val < current2.val: