Posts

CREDIT CARD NUMBER VALIDATOR

Image
CREDIT CARD NUMBER VALIDATOR Run This Program (sample data is stored in data.txt file)   Link to UML and Flowchart This program will validate a credit card number or a file of credit card number based on Luhn Algorithm Run This Program (sample data is stored in data.txt file)   Link to UML and Flowchart

HOW TO GET Nth NODE FROM THE END OF A LINKED LIST

how to get Nth NODE FROM THE END OF A LINKED LIST https://practice.geeksforgeeks.org/problems/nth-node-from-end-of-linked-list/1 solution 1: Time complexity O(n); space complexity O(1) int getNthFromLast(node *head, int n) { node *fa st =head; node * slow =head; for (int i =1; i<n;i++){ if ( fast ==0) return -1; fast =fast->next; } while (fast->next!=0){ fast =fast->next; slow =slow->next; } return slow->data; } solution 2: using stack Space complexity O(n) Time complexity O(n) int getNthFromLast(node *head, int n) { stack< int > s; node *cu = head; int count = 0 ; while (cu != 0 ) { s. push (cu->data); cu = cu-> next ; ++ count ; } if ( count < n) { return - 1 ; } while (n != 1 ) { s. pop (); --n; } return s.top(); }`` int getNthFromLast(node *head, int n) { node *fast = head; node *slow = head; //

Reverse A Linked List in Group of a Given Size

Reverse a Linked List in groups of a given size https://practice.geeksforgeeks.org/problems/reverse-a-linked-list-in-groups-of-given-size/1 Space complexity: O(0); Time complexity: O(n); struct node * reverse (struct node *head, int k) { int n = k; if (head == 0 || head->next == 0 ) return head; struct node * tempHead = head -> next ; head->next = 0 ; struct node * tail = head ; // head will become tail later //reverse K element while (tempHead != 0 && n> 1 ) { struct node * temp = tempHead ; tempHead = tempHead->next; temp->next = head; head = temp; n--; } tail->next = reverse(tempHead, k); return head; } # include <iostream> # include <string> using namespace std ; class node { public : int data; node *next; node( int d) { data = d; next = 0 ; } }; class linkedList { private : node *head; public : linkedList() { head = 0 ; } void a

How to Add 1 To A Number Represented As A LinkedList

HOW TO ADD 1 TO A NUMBER REPRESENTED AS A LINKED LIST A simple solution using a recursive method https://practice.geeksforgeeks.org/problems/add-1-to-a-number-represented-as-linked-list/1 Node *addOne(Node *head) { // Your Code here if (head-> data >= 10 ){ Node *newHead=new Node; newHead -> data = 1 ; head -> data =head-> data % 10 ; newHead -> next=head; head=newHead; return head; } if (head-> next== 0 ) { head -> data =head-> data + 1 ; return head; } else { N ode * temp=addOne(head-> next); head -> data =head-> data +temp-> data / 10 ; temp -> data =temp-> data % 10 ; } return head; }

HACKERRANK EQUAL STACK

HACKERRANK-EQUAL STACK The following function is used to solve the problem  https://www.hackerrank.com/challenges/equal-stacks/problem #include <bits/stdc++.h> #include <string> #include <iostream> #include <vector> using namespace std ; static int t1 = 0 ; static int t2 = 0 ; static int t3 = 0 ; vector < string > split_string ( string ); /* * Author: Y Nguyen * equalStacks function with recursive method * * Space Complexity O(1) */ int equalStacks ( vector < int > & h1 , vector < int > & h2 , vector < int > & h3 ) { /* * Write your code here. */ if ( t1 == t2 && t1 == t3 ) return t1 ; // find the highest town else { if ( t1 >= t2 && t1 >= t3 ) { // remove the first cylinder of the higest stack t1 = t1 - h1 . back (); h1 . pop_back (); } else if ( t2 >= t1 &&