• 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

Generate next palindrome number

Facebook Interview Question : Interleave List

The Magic HackerEarth Nirvana solutions Hiring Challenge

Calculate price of parking from parking start end time prices

Fibonacci Hashing & Fastest Hashtable

Find and print longest consecutive number sequence in a given sequence in O(n)

Add Sub Multiply very large number stored as string

Maximum difference between two elements s.t larger element appears after the smaller number

Urban Ladder Written Test.

Given a string, find the first character which is non-repetitive

Length of the longest substring without repeating characters

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

K’th Largest Element in BST when modification to BST is not allowed

Adobe Interview Questions 8 month Exp

ADOBE Aptitude C Language Test

Count Possible Decodings of a given Digit Sequence

HackeEarth Flipkart’s Drone

BFS (Breath First Search)

The greedy coins game Dynamic Programming

Find Nearest Minimum number in left side in O(n)

Maximum path sum between two leaves

SAP Hiring Off-Campus General Aptitude

Templates in C++

Serialise Deserialise N-ary Tree

SAP Off Campus Hiring_ March 2015 Verbal Skills

Minimum insertions to form a palindrome

How strtok() Works

Reliance Jio Software Developer Interview Experience

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

Introduction To Number Theory ( Part 1 )

Copyright © 2025 · Genesis Framework · WordPress · Log in