
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.
Editorial
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. |
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()
orsort()