PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

Find middle element of linked list

(No Ratings Yet)

November 4, 2012 by asinghal 1 Comment

How to Find the middle element of a linked list without traversing twice ?

Linked list is singly linked list(there is only next pointer no previous pointer).

See Solution: Find middle element of a linked listHide Solution

Take two pointers both pointing to head of the linked list( pointer1 and pointer2 ). Now increment pointer1 by one node and pointer2 by two nodes at a time in a while loop till pointer 2 reaches end of the list or becomes null, at this point the node which pointer1 is pointing to will be the middle element/node of the linked list.

Code in C/C++

Node* getMiddleElement(Node * node){
	Node* pointer1 = node;
	Node* pointer2 = node;

	//traverse till pointer2 reaches end of the list or becomes null
	while(pointer2 != null && pointer2->next != null){
		pointer1 = pointer1->next;
		pointer2 = pointer2->next->next;
	}
	return pointer1;
}
  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
Find nth element from end of linked list

Checkout more Interview Questions Tags: Easy, Interview, Solved Puzzles

Comments

  1. Rohit5 says

    January 10, 2013 at 3:55 am

    Cool. By the way if you want to do it in Java than here is a sample program to find middle element of LinkedList in Java

    Reply

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • 2nd smallest number from 32 numbers
  • Minesweeper Master Solution – Google Code jam 2014
  • Objective Questions set C/C++ – Part 5
  • Full Binary Tree Solution – Google Code Jam
  • Spinning Disc Direction Puzzle
  • Card Game solution: Facebook hacker cup 2013 Round1
  • Find the Min solution: Facebook hacker Cup 2013 Qual Round
  • DS with insert, delete and getRandomElement in O(1)
  • Consonants Solution: Google codejam 2013 Round 1C
  • Security problem solution: Facebook hacker cup 2013 Round 1

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous