-
Notifications
You must be signed in to change notification settings - Fork 242
Array to Linked List
TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)
TIP102 Unit 5 Session 2 Advanced (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Linked Lists, Array to Linked List Conversion
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What happens if the array is empty?
- The function should return
None
.
- The function should return
- What happens if the array contains only one element?
- The function should return a linked list with a single node.
HAPPY CASE
Input: arr = [Player("Mario", "Mushmellow"), Player("Luigi", "Standard LG"), Player("Peach", "Bumble V")]
Output: A linked list with nodes for Mario -> Luigi -> Peach
Explanation: The array is converted into a linked list where each Player instance is a node.
EDGE CASE
Input: arr = []
Output: None
Explanation: When the array is empty, the function returns `None`.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array to Linked List conversion problems, we want to consider the following approaches:
- Creating linked list nodes from array elements
- Sequentially linking the nodes
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Convert each element in the array into a node in the linked list and link them sequentially.
1) Check if the array is empty. If it is, return `None`.
2) Create the head of the linked list using the first element of the array.
3) Initialize a pointer `current` to the head.
4) Iterate through the rest of the array:
a) For each element, create a new node.
b) Link the current node to the new node.
c) Move the pointer to the new node.
5) Return the head of the linked list.
- Forgetting to handle the case where the array is empty.
- Incorrectly linking nodes, resulting in broken or circular lists.
Implement the code to solve the algorithm.
class Player:
def __init__(self, character, kart):
self.character = character
self.kart = kart
self.items = []
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# For testing
def print_linked_list(head):
current = head
while current:
print(current.value.character, end=" -> " if current.next else "\n")
current = current.next
def arr_to_ll(arr):
if not arr:
return None
head = Node(arr[0])
current = head
for player in arr[1:]:
current.next = Node(player)
current = current.next
return head
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Verify the structure of the linked list by printing it after conversion from the array.
Example:
mario = Player("Mario", "Mushmellow")
luigi = Player("Luigi", "Standard LG")
peach = Player("Peach", "Bumble V")
# Convert array to linked list and print
print_linked_list(arr_to_ll([mario, luigi, peach])) # Expected Output: "Mario -> Luigi -> Peach"
# Test with a single element
print_linked_list(arr_to_ll([peach])) # Expected Output: "Peach"
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity: O(N) where N is the number of elements in the array, as we need to process each element.
- Space Complexity: O(N) for storing the linked list nodes created from the array elements.