Category: Linked Lists

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

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