Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same. Method 1 The idea is to get pointer to the last node of list. And then traverse the list starting from the head node and move the odd valued nodes from their current position to end of the list. Algorithm: 1) Get pointer to the last node. 2) Move all the odd nodes to the end. a) Consider all odd nodes before the first even node and move them to end. b) Change the head pointer to point to the first even node. c) Consider all odd nodes after the first even node and move them to the end.
Saturday, September 3, 2011
Segregate even and odd nodes in a Linked List
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment