I deliberately did not include Python solution in this video. It is bit complicated due to the limitation of python. Nonetheless, below is the working code. Hope it helps. nodes = dict() def insert(head, data): NEW = Node(data) NEW.npx = None nodes[id(NEW)] = NEW if head: NEW.npx = id(head) if head.npx: head.npx = id(NEW) ^ head.npx else: head.npx = id(NEW) return NEW
def getList(head): ans = [] prvs = 0 curr = head while curr: ans.append(curr.data) if curr.npx: npx = prvs ^ curr.npx else: break if npx not in nodes: break NEXT = nodes[npx] prvs = id(curr) curr = NEXT
nodes = dict() def insert(head, data): NEW = Node(data) NEW.npx = None nodes[id(NEW)] = NEW if head: NEW.npx = id(head) if head.npx: head.npx = id(NEW) ^ head.npx else: head.npx = id(NEW) return NEW
def getList(head): ans = [] prvs = 0 curr = head while curr: ans.append(curr.data) if curr.npx: npx = prvs ^ curr.npx else: break if npx not in nodes: break NEXT = nodes[npx] prvs = id(curr) curr = NEXT
Nice explanation
Table of Contents
0:00 Problem Statement
0:47 Solution
3:37 Pseudo Code - Insert
6:46 Pseudo Code - get_list
8:29 Code
i appreciate your explanation❤... could you please provide python code??
I deliberately did not include Python solution in this video. It is bit complicated due to the limitation of python. Nonetheless, below is the working code. Hope it helps.
nodes = dict()
def insert(head, data):
NEW = Node(data)
NEW.npx = None
nodes[id(NEW)] = NEW
if head:
NEW.npx = id(head)
if head.npx:
head.npx = id(NEW) ^ head.npx
else:
head.npx = id(NEW)
return NEW
def getList(head):
ans = []
prvs = 0
curr = head
while curr:
ans.append(curr.data)
if curr.npx:
npx = prvs ^ curr.npx
else:
break
if npx not in nodes:
break
NEXT = nodes[npx]
prvs = id(curr)
curr = NEXT
return ans
struct Node *insert(struct Node *head, int data) {
Node *NEW = new Node(data);
NEW->npx = head;
if (head)
head->npx = XOR(NEW, head->npx);
return NEW;
}
vector getList(struct Node *head) {
vector ans;
Node *prvs = NULL;
Node *curr = head;
while (curr) {
ans.push_back(curr->data);
Node *next = XOR(prvs, curr->npx);
prvs = curr;
curr = next;
}
return ans;
}
nodes = dict()
def insert(head, data):
NEW = Node(data)
NEW.npx = None
nodes[id(NEW)] = NEW
if head:
NEW.npx = id(head)
if head.npx:
head.npx = id(NEW) ^ head.npx
else:
head.npx = id(NEW)
return NEW
def getList(head):
ans = []
prvs = 0
curr = head
while curr:
ans.append(curr.data)
if curr.npx:
npx = prvs ^ curr.npx
else:
break
if npx not in nodes:
break
NEXT = nodes[npx]
prvs = id(curr)
curr = NEXT
return ans