Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Linked List Odd Numbers
00:00
5 left

Linked List Odd Numbers

EasyPython

Problem

Agoda processes some itinerary data as a singly linked list. Given the head of the list, return a Python list containing every odd-valued node, preserving the order in which the nodes appear.

Traverse the linked list once. Do not modify or reorder the linked list. If the list is empty or contains no odd values, return an empty list.

Formal Specification

The input is head, either None or a reference to a singly linked-list node with two fields:

  • val: an integer
  • next: a reference to the next node or None

Return a standard Python list of integers containing exactly the values for which value % 2 != 0.

For the examples and test cases, an input array such as [4, 7, 2] represents the linked list 4 -> 7 -> 2. The evaluation harness constructs the linked-list nodes before calling the function.

Constraints

  • 0 <= n <= 10^4, where n is the number of nodes
  • -10^9 <= node.val <= 10^9
  • The list contains no cycles
  • The input linked list must not be modified

Function Signature

def get_odd_values(head):
Interviewer

Your question is Linked List Odd Numbers. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.