• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

December 14, 2014 by Dhaval Dave

Given LinkedList and K=2, N=41->2->3->4->5->6->7->8->9

so Partitioned LinkedList will be

1->2->3->4->5->6->7->8->9

Now delete first K nodes from each subpart and return linked list

3->4->7->8

Here first 2 nodes are deleted from each subparts.

Logic is simple

Keep counter to count occurrence of node in LinkedList
if Count<=K => delete this node.

But Practical its tricky

Solution

#include<stdio.h>
#include<iostream>
#include <stdlib.h>
using namespace std;
struct node
{
    int data; //data belong to that node
    node *next; //next pointer
};
void push(node **head, int data)
{
    node *newnode = new node;
    newnode->data = data;
    newnode->next = *head;
    *head = newnode;
}
void printList(struct node *head)
{
    struct node *temp = head;
    while (temp != NULL)
    {
        printf(“%d-> “, temp->data);
        temp = temp->next;
    }
    //printf(“n”);
}
int deleteKafterN(struct node *head,int k, int n){

struct node *prev=NULL, *curr=NULL ,*temp =NULL;
int count=1;
prev=NULL;
curr=head;

printf(“nn=%d and k=%dn”,n,k);

//In Starting delete first K nodes and set Head.
for(int i=0;i<k;i++){
prev=head;
head = head->next;
free(prev);
count++;
}
if(head->next){prev=head;curr=head->next;count++;}

//Till next head of subLL increment pointers

while(count<n+1){
    curr=curr->next;
    prev=prev->next;
    count++;
}
printf(“n”);
while(curr){
if(count == n+1){ count=1; }
if(count<=k){
prev->next=curr->next;
temp=curr;
curr=curr->next;
free(temp);
}
else {
                prev=prev->next;
curr=curr->next;
}
count++;

}
//while

printList(head);
}
//deleteKafterN

int main()
{
    node *head1 = NULL;
    push(&head1, 60);
    push(&head1, 50);
    push(&head1, 40);
    push(&head1, 30);
    push(&head1, 20);
    push(&head1, 10);
    push(&head1, 9);
    push(&head1, 3);
    push(&head1, 1);
    printList(head1);
    deleteKafterN(head1,2,4);
   
    return 0;
}
Working Code : http://ideone.com/Zt79kh
posted by Dhaval

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Linked List

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Word Break Problem

DFS (Depth First Search)

TicTacToe Game As Asked in Flipkart

Fibonacci Hashing & Fastest Hashtable

Maximum sum contiguous subarray of an Array

Amazon Interview Experience – SDE Chennai

Amazon Interview On-Campus For Internship – 1

Urban Ladder Written Test.

Circular Linked List

Level order traversal in Spiral form

1014 Practice Question of New GRE – Princeton

Maximum occurred Smallest integer in n ranges

Binary Tree Isomorphic to each other

Diagonal Traversal of Binary Tree

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Printing intermediate Integers between one element & next element of array

Print all nodes that are at distance k from a leaf node

Calculate price of parking from parking start end time prices

Stock Buy Sell to Maximize Profit

Find Pythagorean Triplets in an array in O(N)

Given Set of words or A String find whether chain is possible from these words or not

Coin Collection Dynamic Programming

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Reliance Jio Software Developer Interview Experience

Check Binary Tree is Binary Search Tree or not

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

simple sql injection

Generate largest number arranging a no. of given non negative integer numbers

Flipkart Set 1 On Campus with Answers

Right view of Binary tree

Copyright © 2023 · Genesis Framework · WordPress · Log in