if the first linked list is empty, all the nodes from the second linked list should be added to the first one. But the first linked list remains empty.
To solve this problem you should change the following code of block:
if not p: return q if not q: return p
if not p: self.head = q return self.head if not q: return self.head