PuzzlersWorld.com

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

Find middle element of linked list

(1 votes, average: 1.00 out of 5)

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

  • Find Number present only once
  • Reverse a link list without using another
  • Osmos Solution: Google codejam 2013 Round 1B
  • Cake Cutting solution: Facebook hacker cup 2013 Round 2
  • Spinning Disc Direction Puzzle
  • Subjective Question set C/C++ – Part 1
  • Find nth element from end of linked list
  • Bullseye Solution: Google codejam 2013 Round 1A
  • Find a pythagorean triplet from an array
  • Lawnmower Solution: Google codejam 2013 Qual Round

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