-
Notifications
You must be signed in to change notification settings - Fork 247
Creating and Linking Nodes
JCSU Unit 9 Problem Set 1 (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Linked Lists, Node Creation, Pointers
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 is the goal of the problem?
- Create two linked list nodes and link them sequentially.
- Are there constraints on input?
- The
Node
class is provided, and the problem assumes valid input.
- The
HAPPY CASE Input: node_one = Node("a") node_two = Node("b") node_one.next = node_two Output: node_one.value = "a" node_one.next.value = "b" node_two.value = "b" node_two.next = None
EDGE CASE
Input:
Only one node created: Node("a")
Output:
node.next = None
Explanation:
Without a second node to link, next
remains as 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 node creation and linking problems, we want to consider the following approaches:
-
Node Creation and Assignment: Instantiate
Node
objects and use theirnext
property to link them.
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Use the Node
class to create two nodes and set the next
property of the first node to point to the second node.
- Instantiate
node_one
with the value"a"
. - Instantiate
node_two
with the value"b"
. - Set
node_one.next
to referencenode_two
.
Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value # Store the value of the node
self.next = next # Reference to the next node (defaults to None)
# Step 1: Create two nodes
node_one = Node("a") # Create the first node with value "a"
node_two = Node("b") # Create the second node with value "b"
# Step 2: Link the nodes
node_one.next = node_two # Set the 'next' of node_one to reference node_two
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input: node_one = Node("a"), node_two = Node("b") node_one.next = node_two
- Expected Output: node_one.value = "a" node_one.next.value = "b" node_two.value = "b" node_two.next = None
- Observed Output: node_one.value = "a" node_one.next.value = "b" node_two.value = "b" node_two.next = None
Example 2:
- Input: node = Node("a")
- Expected Output: node.value = "a" node.next = None
- Observed Output: node.value = "a" node.next = None
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the number of nodes created.
- Time Complexity: O(1) for creating and linking each node, as operations are constant-time.
- Space Complexity: O(1) additional space beyond the storage required for the nodes themselves.