• 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

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

ADOBE Aptitude C Language Test

Implement LRU Cache

Stickler thief

Find loop in Singly linked list

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Skiing on Mountains Matrix

Convert number to words java

Amazon Interview On-Campus For Internship – 1

Flipkart Set 1 On Campus with Answers

Serialise Deserialise N-ary Tree

Spanning Tree

Edit Distance ( Dynamic Programming )

TicTacToe Game As Asked in Flipkart

Trie Dictionary

Printing intermediate Integers between one element & next element of array

Find min element in Sorted Rotated Array (With Duplicates)

Amazon Interview Experience – SDE Chennai

System Design: Designing a LLD for Hotel Booking

CodeChef Code SGARDEN

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

Sort Stack in place

Code Chef PRGIFT Solution

Handle duplicates in Binary Search Tree

Cisco Hiring Event 21st – 22nd Feb 2015

C++ OOPs Part1

Maximum size of square sub matrix with all 1’s in a binary matrix

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

SAP Off Campus Hiring_ March 2015 Sample Questions

Find the kth number with prime factors 3, 5 and 7

Copyright © 2025 · Genesis Framework · WordPress · Log in