How to traverse data in a linked list in forward and backward direction, write the algorithm?



How to traverse data in a linked list in forward and backward direction, write the algorithm?..

Answer / Bhagat Singh

To traverse a LinkedList in forward direction:
1. Get the head of the list (first node).
2. Until the current node is not null, move to the next node by accessing its 'next' reference.
3. Print or perform an operation on each node as you visit it.

To traverse a LinkedList in backward direction:
1. Get the last node of the list (last node is the one with null 'next' reference).
2. Until the current node is not the head, move to the previous node by accessing its 'previous' reference (if available) or the current node's 'next' reference (if 'previous' is not available).
3. Print or perform an operation on each node as you visit it.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Data Structures Interview Questions

Write the syntax in c to create a node in the singly linked list.

1 Answers  


What do you mean by sorting data?

1 Answers  


Define a stack?

1 Answers  


What is variable size arrays?and why we use it?

1 Answers   MCN Solutions,


How do you declare A pointer to array of three chars

1 Answers  


What is the difference between data types and data structures?

1 Answers  


What is difference between arraylist and linkedlist?

1 Answers  


Let the G be a graph with 100 vertices numbered 1 to 100 Two vertices i and j are adjecnt if | i-j| =8 or | i-j| =12. The Number of connected components in G is ?

4 Answers  


Write the importance of hashing?

1 Answers  


Give the example of validating the parenthesis of expression using stack.

1 Answers  


What is bubble sort?

1 Answers  


How do I sort a hashmap key?

1 Answers  


Categories