• 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 a sorted array and a number x, find the pair in array whose sum is closest to x

Python Dictionaries

Test Cases for Round Function

Reversal of LinkedList

Closed Parentheses checker

Practo Hiring Experience

Fibonacci Hashing & Fastest Hashtable

Trie Dictionary

Binary Tree in Java

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

Maximum sum contiguous subarray of an Array

Amazon Interview On-Campus For Internship – 1

Number of Islands BFS/DFS

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

LeetCode : Word Search

Diagonal Traversal of Binary Tree

HackeEarth Flipkart’s Drone

Serialise Deserialise N-ary Tree

Stock Buy Sell to Maximize Profit

Singly linked list

How Radix sort works

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Minimum insertions to form a palindrome

Common Ancestor in a Binary Tree or Binary Search Tree

Level order traversal in Spiral form

strtok()

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

Check Binary Tree is Binary Search Tree or not

Copyright © 2025 · Genesis Framework · WordPress · Log in