[Python] code accepted, but confusion about data type for "next"

  • 0

    I was able to get the code accepted, but there's one thing I'm confused about - in Python, what exactly is the data type for the "next" field, is it a pointer / reference, or is it a copy?

    Specifically, with the given class definition

    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None

    If I were to start from scratch and connect two existing nodes (n1=ListNode(0), n2=ListNode(0)), would the code be: n1.next = n2 ? Or something like n1.next = &n2 ? Is it necessary at all to be explicit about a copy of variable or a reference to a variable?


  • 0

    Hi, IMHO, it seems to be a ListNode

  • 0

    It's supposed to reference a ListNode, so yes, do n1.next = n2. That &n2 or something like it doesn't even exist in Python.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.