Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Code
Diff
  • class Node:
        def __init__(self, x, Next, Prev):
            self.data = x
            self.next = Next
            self.prev = Prev
          
        
    class LinkedListIterator:
        def __init__(self, node):
            self.node = node
        
        def __iter__(self):
            return self
                
        def __next__(self):
            if not self.node:
                raise StopIteration
            value = self.node.data
            self.node = self.node.next        
            return value
    
           
    class LinkedList:
        def __init__(self, *x):
            self.start = None
            self.last = None
            self.length = 0
            self.addAll(x)
            
        def __iter__(self):
            return LinkedListIterator(self.start)
            
        def __str__(self):        
            out = '[ '+' '.join(map(str, self))+' ]'
            return out
        
        def clear(self):
            self.start = None
            self.last = None
            self.length = 0
            
        def add(self, x):
            if self.start == None:
                self.start = self.last = Node(x,None,None)
                self.length = 1
            else:
                prev = self.last
                self.last.next = self.last = Node(x,None,None)
                self.last.prev = prev
                self.length += 1
                
        def addAll(self, xs):        
            for i in xs:
                self.add(i)
        
        
        def remove_node(self, node):
            if not node.prev and node.next:            
                self.start = node.next
                node.next.prev = None
                self.length -= 1
                    
            elif not node.next and node.prev:            
                node.prev.next = None
                self.last = node.prev
                self.last.next = None
                self.length -= 1
                
            elif node.prev and node.next:
                node.prev.next = node.next
                node.next.prev = node.prev
                self.length -= 1
            else:
                self.clear()
                                
                
        def remove(self, x, remove_all = False):
            current = self.start        
            while current:
                if current.data == x:
                    self.remove_node(current)
                    if not remove_all:
                        break
                current = current.next
  • 11 class Node:
    2- def __init__(self,x,Next, Prev):
    2+ def __init__(self, x, Next, Prev):
    33 self.data = x
    44 self.next = Next
    55 self.prev = Prev
    6+
    7+
    8+class LinkedListIterator:
    9+ def __init__(self, node):
    10+ self.node = node
    11+
    12+ def __iter__(self):
    13+ return self
    14+
    15+ def __next__(self):
    16+ if not self.node:
    17+ raise StopIteration
    18+ value = self.node.data
    19+ self.node = self.node.next
    20+ return value
    66
    22+
    77 class LinkedList:
    88 def __init__(self, *x):
    99 self.start = None
    1010 self.last = None
    11- self.lenght = 0
    12- if x:
    13- for i in x:
    14- self.add(i)
    27+ self.length = 0
    28+ self.addAll(x)
    1515
    16- def __str__(self):
    17- if self.start != None:
    18- current = self.start
    19- out = '[ ' + str(current.data)
    20- while current.next != None:
    21- current = current.next
    22- out += ' '+ str(current.data)
    23- out += ' ]'
    24- return out
    30+ def __iter__(self):
    31+ return LinkedListIterator(self.start)
    2525
    33+ def __str__(self):
    34+ out = '[ '+' '.join(map(str, self))+' ]'
    35+ return out
    36+
    2626 def clear(self):
    27- self.__init__()
    38+ self.start = None
    39+ self.last = None
    40+ self.length = 0
    2828
    2929 def add(self, x):
    3030 if self.start == None:
    3131 self.start = self.last = Node(x,None,None)
    32- self.lenght += 1
    45+ self.length = 1
    3333 else:
    3434 prev = self.last
    3535 self.last.next = self.last = Node(x,None,None)
    3636 self.last.prev = prev
    37- self.lenght += 1
    50+ self.length += 1
    3838
    39- def addFew(self,*x):
    40- if x:
    41- for i in x:
    42- self.add(i)
    52+ def addAll(self, xs):
    53+ for i in xs:
    54+ self.add(i)
    4343
    69+ elif node.prev and node.next:
    70+ node.prev.next = node.next
    71+ node.next.prev = node.prev
    72+ self.length -= 1
    73+ else:
    74+ self.clear()
    75+
    4444
    45- def remove(self, x,remove_all = False):
    46- current = self.start
    47-
    48- for i in range(self.lenght):
    77+ def remove(self, x, remove_all = False):
    78+ current = self.start
    79+ while current:
    4949 if current.data == x:
    50- if current.prev == None:
    51- if current.next != None:
    52- self.start = current.next
    53- current.next.prev = None
    54- self.lenght -= 1
    81+ self.remove_node(current)
    82+ if not remove_all:
    7272 break
    7373 current = current.next