XOR Linked List | GeeksForGeeks | Problem of the Day

Поділитися
Вставка
  • Опубліковано 5 лис 2024

КОМЕНТАРІ • 6

  • @AshutoshKumar-es8xy
    @AshutoshKumar-es8xy Місяць тому +1

    Nice explanation

  • @mathematics3398
    @mathematics3398  Місяць тому

    Table of Contents
    0:00 Problem Statement
    0:47 Solution
    3:37 Pseudo Code - Insert
    6:46 Pseudo Code - get_list
    8:29 Code

  • @chakrismart4546
    @chakrismart4546 Місяць тому +2

    i appreciate your explanation❤... could you please provide python code??

    • @mathematics3398
      @mathematics3398  29 днів тому

      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

  • @mathematics3398
    @mathematics3398  Місяць тому

    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;
    }

  • @mathematics3398
    @mathematics3398  29 днів тому

    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