• 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

Level order traversal in Spiral form

Generic Object Oriented Stack with Template

Find loop in Singly linked list

Print vertical sum of all the axis in the given binary tree

SAP Off Campus Hiring_ March 2015 Sample Questions

Trie Dictionary

Templates in C++

BlueStone E-commerce Interview Experience

Python List

Common Ancestor in a Binary Tree or Binary Search Tree

C++ OOPs Part1

Check if an array has duplicate numbers in O(n) time and O(1) space

Fibonacci Hashing & Fastest Hashtable

How Radix sort works

Regular Expression Matching

Client Server C program

Advanced SQL Injection

System Design: Designing a LLD for Hotel Booking

Leetcode: Merge Intervals

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

VMWare SDEII Interview

Find the smallest window in a string containing all characters of another string

Maximum sum contiguous subarray of an Array

SAP Off Campus Hiring_ March 2015 Computer Skills

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

Memory Efficient LinkedList

Cisco Hiring Event 21st – 22nd Feb 2015

FizzBuzz Solution C C++

Reliance Jio Software Developer Interview Experience

Printing intermediate Integers between one element & next element of array

Copyright © 2025 · Genesis Framework · WordPress · Log in