• 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

simple sql injection

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Maximum path sum between two leaves

HackeEarth Flipkart’s Drone

Possible sizes of bus to carry n groups of friends

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

Reliance Jio Software Developer Interview Experience

Calculate price of parking from parking start end time prices

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

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

Maximum sum contiguous subarray of an Array

Find next greater number with same set of digits

How Radix sort works

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

CodeChef Code SGARDEN

Singly linked list

Trapping Rain Water

SAP Off Campus Hiring_ March 2015 Computer Skills

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Knight Tour Problem (Graph – Breadth First Search)

Printing intermediate Integers between one element & next element of array

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

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Count Possible Decodings of a given Digit Sequence

FizzBuzz Solution C C++

Print Power Set of a Set

Sort an array according to the order defined by another array

Generate largest number arranging a no. of given non negative integer numbers

Stickler thief

Copyright © 2026 · Genesis Framework · WordPress · Log in