Summary
Add methods to insert nodes at the head and tail of a singly linked list.
Problem statement
Extend a simple LinkedList with two operations: - prepend(value): insert a new node at the beginning of the list. - append(value): insert a new node at the end of the list. Also implement apply_operations(values, ops) which builds a LinkedList from the initial values list and applies ops in order. ops is a list of tuples where each tuple is of the form (operation_name, value) and operation_name is either 'append' or 'prepend'. Return the LinkedList instance after all operations. The LinkedList __str__ should show 'a->b->c' or '' for empty.
Task
Implement append(value) and prepend(value) methods for a LinkedList and a helper to apply operation sequences.
Examples
Append then prepend
Input
values = [1,2]; ops=[('append',3), ('prepend',0)]
Output
0->1->2->3
Explanation
Start with 1->2, append 3 to get 1->2->3, then prepend 0 to get 0->1->2->3.
Input format
values: Python list; ops: list of tuples like [('append', 3), ('prepend', 0)].
Output format
A LinkedList instance; str() shows nodes joined with '->'.
Constraints
Ops contain only 'append' or 'prepend'. Do not use built-in deque behaviors for the linked-list operations; manipulate nodes directly.
Samples
Sample input 0
([], [('prepend', 1)])
Sample output 0
1
Explanation 0
Prepending to an empty list creates a single node.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.