0
def connect(self, new_sll):
    pre = self.head
    if sll is SinglyLL():
        pre = self.head
        for i in range(self.nodenumber):
            pre = pre.next
            
            if pre == None :
                for i in range(i + new_sll.nodenumber):
                    aft = pre.next
                    new = sll.head.data
                    node = Node(new)
                    node.next = aft
                    pre.next = node
                    self.nodenumber +=1

    
    else :
        raise TypeError("error")

I'm trying to connect an existing singly-linked-list (sll) and another singly-linked-list (new_sll).

If new_sll's class type is SinglyLL, I want to connect new_sll with the existing sll and renew the node's number.

If new_sll's class type is not SinglyLL, I want to show a type error

I can't solve this matter... how can I fix my code?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
ricollob
  • 1
  • 2
  • Don't use `is` ([Understanding the "is" operator](https://stackoverflow.com/questions/13650293/understanding-the-is-operator)) and don't do `SinglyLL()` which creates _a new object_ and doesn't do "*If new_sll's class type is SinglyLL*". – Gino Mempin Apr 27 '22 at 07:52
  • 1
    Does this answer your question? [How to check if variable is a specific class in python?](https://stackoverflow.com/questions/18117729/how-to-check-if-variable-is-a-specific-class-in-python) – Gino Mempin Apr 27 '22 at 07:52
  • Also, the term and the tag [sll](https://stackoverflow.com/tags/sll/info) means something else, and it may be a confusing abbreviation for [singly-linked-list]. So I edited to expand the term to the full "singly-linked-list". – Gino Mempin Apr 27 '22 at 07:58

1 Answers1

0

There are several issues in your code:

  • sll is SinglyLL() can never be true, as SinglyLL() creates a new list, which obviously is not the one that sll represents.

  • Assuming that nodenumber is the number of nodes in the list, the condition pre == None is always going to be true, but then aft = pre.next will raise an error. As pre is None, the outer loop loses its benefit completely, and it becomes impossible to make the link

  • new = sll.head.data is always going to get the same data, in each iteration of the loop.

  • The name sll and new_sll are not the same, yet it seems the same is intended

  • Raising an error with description "error" is not going to help anyone.

Here is a correction:

def connect(self, sll):
    if not isinstance(sll, SinglyLL):
        raise TypeError("The connect method should be called with an SinglyLL instance as argument")
    other = sll.head
    if not other:
        return
    if self.head is None:
        self.head = Node(other.data)
        other = other.next
    node = self.head
    while node.next:
        node = node.next
    while other:
        node.next = Node(other.data)
        node = node.next
        other = other.next
    self.nodenumber += sll.nodenumber
trincot
  • 317,000
  • 35
  • 244
  • 286