Category: Linked List

Check if all elements of given Linked List corresponds to a downward path from any node in given Binary Tree

  #include “bits/stdc++.h”using namespace std;  struct listNode {    int val;    struct listNode* next;          listNode(int data)    {        this->val = data;        next = NULL;    }};  struct treeNode {    int val;    treeNode* right;    treeNode* left;          treeNode(int data)    {        this->val = data;        this->left = NULL;        this->right = NULL;    }};  bool isPathUtil(listNode* curList,                treeNode* curTree){            if (curList == NULL)        return true;          if (curTree == NULL)        return false;      if (curList->val == curTree->val) {                          return isPathUtil(curList->next,                          curTree->left)               || isPathUtil(curList->next,                             curTree->right);    }    else {                  return false;    }}  bool isPath(listNode* head, treeNode* root){              if (root == NULL)        return false;              if (head == […]

Continue Reading

Find direction of path followed from root by a linked list in a Binary Tree

#include using namespace std;  struct ListNode {    int data;    ListNode* next;    ListNode(int data)    {        this->data = data;        this->next = NULL;    }};  struct TreeNode {    TreeNode* left;    TreeNode* right;    int val;      TreeNode(int x)        : left(NULL), right(NULL), val(x)    {    }};  ListNode* makeList(int arr[], int n){    ListNode* h = NULL;    ListNode* root;    for (int i = 0; i < n; i++) {        int data = arr[i];        ListNode* node = new ListNode(data);          if (h == NULL) {            h = node;            root = h;        }        else {            root->next […]

Continue Reading

Form a Rectangle from boundary elements of Matrix using Linked List

class Node:          def __init__(self, val):        self.data = val        self.next = None        self.prev = None        self.top = None        self.bottom = None    class LinkedList:          def __init__(self):        self.head = None              def Quad(self, grid, n, m):                  self.head = Node(grid[0][0])                  temp = self.head                  i = 0        j = 1                  while temp.top != self.head:                                                                                      if j < m and i == 0:                temp.next = Node(grid[i][j])                temp = temp.next                j += 1                                      elif j == m and i < n - 1:                i […]

Continue Reading

Reverse a singly Linked List in groups of given size | Set 4 (Space efficient approach)

  class LinkedList {          Node head;          class Node {        int data;        Node next;        Node(int d)        {            data = d;            next = null;        }    }      Node reverse(Node node, int k)    {        if (k == 1)            return node;                  Node result = null;                  Node head = new Node(0);        head.next = node;        Node next = node.next;          int count = 0;        while (next != null) {            count++;                                                  Node tmp = next.next;            next.next = node;            node = next;            next = tmp;              if (count == k – 1) {                count […]

Continue Reading

Multiply a single digit number in place to a number represented as a Linked List

#include using namespace std;  class Node {public:    int data;    Node* next;};  Node* newNode(int data){        Node* new_node = new Node;          new_node->data = data;    new_node->next = NULL;          return new_node;}  Node* reverse(Node* head){    Node* prev = NULL;    Node* current = head;    Node* next;          while (current != NULL) {        next = current->next;        current->next = prev;        prev = current;        current = next;    }              return prev;}  Node* multiplyHelp(Node* head, int M){        Node* res = head;          Node* prev = NULL;          int carry = 0, product […]

Continue Reading

Partition a Linked List into 3 parts such that the maximum difference between their sizes is minimum

  #include using namespace std;  class Node {public:    int data;    Node* next;};  int sizeOfLL(Node* head){    int size = 0;          while (head != NULL) {        ++size;        head = head->next;    }    return size;}  vector Partition_of_list(Node* head){    int size = sizeOfLL(head);    Node* temp = head;    vector ans;          if (3 >= size) {                        while (temp != NULL) {            Node* next = temp->next;            temp->next = NULL;            ans.push_back(temp);            temp = next;        }                                  int y = 3 – size;        while (y != 0) {            ans.push_back(NULL);            y–;        }    }    else {                int minSize […]

Continue Reading

Modify a Linked List to contain last occurrences of every duplicate element

#include using namespace std;  class Node {public:    int data;    Node* next;      Node(int x)    {        this->data = x;        this->next = NULL;    }};  Node* reverseList(Node* head){    Node *prev = NULL, *nextNode = NULL;    while (head != NULL) {                          nextNode = head->next;                          head->next = prev;          prev = head;          head = nextNode;    }    return prev;}  Node* Remove_Dup_Keep_Last_Occurence(    Node* head){        Node* dummy = new Node(-1);    dummy->next = head;          dummy->next = reverseList(dummy->next);          unordered_set visited;      Node *currNode = dummy, *nextNode;          while (currNode != NULL           && currNode->next != NULL) […]

Continue Reading

Subtract 1 from a number represented as Linked List

  #include using namespace std;  class Node {public:    int data;    Node* next;};  Node* newNode(int data){        Node* new_node = new Node;    new_node->data = data;    new_node->next = NULL;          return new_node;}  int subtractOneUtil(Node* head){          if (head == NULL)        return -1;              int borrow = subtractOneUtil(        head->next);          if (borrow == -1) {                          if (head->data == 0) {            head->data = 9;            return -1;        }                          else {            head->data = head->data – 1;            return 0;        }    }          else {        return 0;    }}  Node* subtractOne(Node* head){              subtractOneUtil(head);              while (head and head->next           and head->data == 0) […]

Continue Reading

Partition a Linked List into K continuous groups with difference in their sizes at most 1

  #include using namespace std;  struct ListNode {    int val;    struct ListNode* next;};  void push(ListNode** head_ref,          int node_val){        ListNode* new_node = new ListNode();          new_node->val = node_val;          new_node->next = (*head_ref);          (*head_ref) = new_node;}  void splitListInParts(ListNode* head,                      int K){        vector ans;          if (!head) {                  while (K–)            ans.push_back(NULL);    }              int N = 0;              ListNode* p = head;          while (p) {                  p = p->next;                  N++;    }      int len = N / K;    int rem = N % K;      p = head;          while (K > 0 && […]

Continue Reading