We saw that behind the scenes, Arrays don't store raw data inside of them - instead, they store pointers to their data. It turns out that every data structure stores pointers to their data. Everything's made up of pointers! Even variables are pointers in Python!
The amazing result of this is that whenever you write A = B
, Python points A
to B
. There are no exceptions.
Here's an example where we run one line of code at a time, and we show you the picture you should have in mind as the code runs.
Note that as the code ran, we changed y, but x changed too! This is confusing behavior to beginners, but it's easy to understand once you know everything's made of pointers in Python, and A = B
always points A
to B
.
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()