• 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

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

Implement LRU Cache

Leetcode: Edit Distance

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Urban Ladder Written Test.

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

Find if a binary tree is height balanced ?

ADOBE Aptitude C Language Test

Naurki.com Security Breach

C Program for TAIL command of UNIX

VMWare SDEII Interview

Common Ancestor in a Binary Tree or Binary Search Tree

Advanced SQL Injection

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

25 horses 5 tracks Find 3 fastest puzzle

Doubly linked list

TicTacToe Game As Asked in Flipkart

Hackerearth : Counting Subarrays

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

Flipkart SDET Interview Experience

DFS (Depth First Search)

Reversal of LinkedList

Handle duplicates in Binary Search Tree

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

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

Length of the longest substring without repeating characters

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

Microsoft BING Interview Experience

Spanning Tree

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

Copyright © 2025 · Genesis Framework · WordPress · Log in