예제 #1
0
    def insert(self, node, value):
        'insert valut after node specified'
        t = Node(value)
        n = node.next
        if n is not None:
            t.next = n
            n.pre = t

        node.next = t
        t.pre = node
        return node
예제 #2
0
 def append(self, value):
     if self.__first is None and self.__last is None:
         self._first = Node(value)
         self.__last = self.__first
     elif self.__first == self.__last:
         n = Node(value)
         self.__last = n
         self.__last.pre = self.__first
         self.__first.next = self.__last
     else:
         n = Node(value)
         self.__last.next = n
         n.pre = self.__last
         self.__last = n
예제 #3
0
 def insert(self, pos, item):
     index = 0
     current = self.head
     pre = None
     temp = Node(item)
     if pos == index:
         self.add(item)
     else:
         while current != None:
             index += 1
             pre = current
             current = current.getNext()
             if index == pos:
                 temp.setNext(current)
                 pre.setNext(temp)
                 return
         return "Could not insert into the LinkList"
예제 #4
0
    def copyRandomList(self, head: 'Node') -> 'Node':
        if not head:
            return head
        dumpy = head
        while dumpy:
            new_node = Node(dumpy.val, next=dumpy.next)
            dumpy.next = new_node
            dumpy = dumpy.next.next
        dumpy = head
        while dumpy:
            new_node = dumpy.next
            new_node.random = dumpy.random.next if dumpy.random else None
            dumpy = dumpy.next.next

        new_head = head.next
        dumpy = head
        while dumpy:
            new_node = dumpy.next
            dumpy.next = dumpy.next.next
            new_node.next = new_node.next.next if new_node.next else None
            dumpy = dumpy.next
        return new_head
예제 #5
0
 def add(self, item):
     current = self.head
     pre = None
     temp = Node(item)
     while current != None:
         if current.getData() > item:
             break
         else:
             pre = current
             current = current.getNext()
     if pre == None:
         temp.setNext(self.head)
         self.head = temp
     else:
         temp.setNext(current)
         pre.setNext(temp)
예제 #6
0
 def test_node(self):
     value = 10
     n = Node(value)
     n.print()
     assert value == n.val
     assert n.next is None
예제 #7
0
 def add(self, item):
     temp = Node(item)
     temp.setNext(self.head)  #设置指向表头的下一个节点
     self.head = temp  #设置表头
예제 #8
0
 def append(self, item):
     current = self.head
     temp = Node(item)
     while current.next != None:
         current = current.getNext()
     current.setNext(temp)