Tutorialspoint
Problem
Solution
Submissions

Linked List from Scratch

Certification: Advanced Level Accuracy: 100% Submissions: 2 Points: 15

Implement a Python class called LinkedList that creates a singly linked list data structure from scratch. The linked list should include methods to insert nodes at the beginning, end, or at a specific position, delete nodes, search for values, and display the list. Each node in the list should contain a value and a reference to the next node.

Example 1
  • Input: linked_list = LinkedList()
  • Operations: linked_list.insert_at_end(10)
  • Operations: linked_list.insert_at_end(20)
  • Operations: linked_list.insert_at_beginning(5)
  • Operations: linked_list.insert_at_position(30, 3)
  • Output: 5 → 10 → 20 → 30 → None
  • Explanation:
    • Step 1: Create a LinkedList instance.
    • Step 2: Insert values at different positions.
    • Step 3: Display the final linked list.
Example 2
  • Input: linked_list = LinkedList()
  • Operations: linked_list.insert_at_beginning("banana")
  • Operations: linked_list.insert_at_beginning("orange")
  • Operations: linked_list.delete("banana")
  • Operations: linked_list.insert_at_end("banana")
  • Output: orange → banana → None
  • Explanation:
    • Step 1: Create a LinkedList instance.
    • Step 2: Insert values at beginning.
    • Step 3: Delete a value.
    • Step 4: Insert a value at end.
    • Step 5: Display the final linked list.
Constraints
  • The linked list can contain elements of any data type.
  • Time Complexity: O(1) for operations at the beginning, O(n) for operations at the end or at a specific position.
  • Space Complexity: O(n) where n is the number of elements in the linked list.
  • Operations on an empty linked list should be handled gracefully.
Linked ListFunctions / MethodsPointers and ReferencesTech MahindraSnowflake
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Create a Node class to represent individual nodes with value and next attributes
  • Implement head and tail pointers for quick access to the beginning and end of the list
  • Handle edge cases like empty lists and operations at the boundaries
  • Consider implementing a length attribute to track the size of the list
  • For search operations, iterate through the list comparing values
  • Use a None reference to indicate the end of the list
  • Consider implementing additional methods like reverse() or sort()

Steps to solve by this approach:

 Step 1: Create a Node class with value and next pointer attributes.
 Step 2: Implement a LinkedList class that maintains a reference to the head node.
 Step 3: Develop insert methods for beginning, end, and specific positions in the list.
 Step 4: Implement a delete method that handles head removal and internal node removal cases.
 Step 5: Add a search method that traverses the list and tracks position of nodes.
 Step 6: Create a display method to visualize the entire linked list as a string.
 Step 7: Handle edge cases like empty lists or positions out of bounds.

Submitted Code :