• 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

Reverse a Linked List in groups of given size

October 9, 2014 by Dhaval Dave

Given a linked list, write a function to reverse every k nodes (where k is an input to the function).

Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.

Solution : Reverse a LinkedList keeping counter, to keep node count along with it.


Algo :

  1. Cur = Head, Next = Null, Pre = Null
  2. While (Curr != NULL AND Counter < k ) //till the end & counter to keep less than input k
    1. next = cur -> nextt
    2. cur-> nextt = pre 
    3. pre = cur
    4. cur = next
    5. count ++;
  3. if ( next !=  NULL)
    1.  head->next = reverse(next, k); 
  4. return prev;

Step 2  : is to reverse a linked list. by keeping K set we are reversing only first K node.

Step 3  : is to reverse next k node, 
so till the end of linked list we are reversing each k nodes.
Step 4  : prev will be head of this linkedlist, return it.

payday loans pennsylvania

Similar Articles

Filed Under: Adobe Interview Questions, Amazon Interview Question, 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

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

Handle duplicates in Binary Search Tree

Reversal of LinkedList

Longest Increasing Subsequence

The Magic HackerEarth Nirvana solutions Hiring Challenge

Common Ancestor in a Binary Tree or Binary Search Tree

Wrong Directions given find minimum moves so that he can reach to the destination

Walmart Labs Interview Experience

Find Pythagorean Triplets in an array in O(N)

CodeChef Code SGARDEN

strtok()

Get K Max and Delete K Max in stream of incoming integers

Naurki.com Security Breach

Printing intermediate Integers between one element & next element of array

Count Possible Decodings of a given Digit Sequence

Coin Collection Dynamic Programming

Sort Stack in place

Reliance Jio Software Developer Interview Experience

Implement LRU Cache

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Doubly linked list

Cisco Hiring Event 21st – 22nd Feb 2015

Sort an array according to the order defined by another array

Binary Tree in Java

Given a float number convert it into the string WITHOUT using any inbuilt Function

1014 Practice Question of New GRE – Princeton

Diagonal Traversal of Binary Tree

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

Count number of ways to reach a given score in a game

Skiing on Mountains Matrix

Copyright © 2025 · Genesis Framework · WordPress · Log in