Write a program to reverse a linked list.
Answer:
Python3
class ListNode: def __init__( self , val = 0 , next = None ): self .val = val self . next = next def reverse_linked_list(head): prev = None current = head while current: next_node = current. next current. next = prev prev = current current = next_node return prev # Example usage head = ListNode( 1 ) head. next = ListNode( 2 ) head. next . next = ListNode( 3 ) reversed_head = reverse_linked_list(head) while reversed_head: print (reversed_head.val, end = " " ) # Output: 3 2 1 reversed_head = reversed_head. next |
Explanation: This function reverses a linked list iteratively by changing the direction of pointers. It maintains three pointers: prev
, current
, and next_node
to reverse the direction of pointers while traversing the list.
Software Developer Interview Questions
Software developer interview questions can cover a wide range of topics, depending on the specific role, company, and level of experience being targeted. The software developer interview questions aim to assess a candidate’s technical skills, problem-solving abilities, communication skills, and fit for the role and the company.
Table of Content
- Interview Questions for Internship and Fresher Level
- Interview Questions for Software Development Engineer SDE 1 level
- Interview Questions for Software Development Engineer SDE 2 level
- Interview Questions for Software Development Engineer SDE 3 level