Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Palindrome or Duplicate Arrays Practice Set

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Palindrome or Duplicate Arrays Practice Set. Start with the requirements on the right.

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.

Problem

nference processing pipelines may represent records as singly linked lists while preserving object identity for downstream consumers. Given the head of a singly linked list, swap the first and second nodes by changing pointers, then return the new head.

Do not swap only the values stored in the nodes. Preserve every node object and the relative order of all nodes after the first two.

Formal Specification

The input is either None or a reference to a ListNode object with fields val and next. The output is the head reference of the same list after the first two nodes are exchanged. The evaluator serializes the resulting list as an array of values. Test case inputs use arrays as a concise serialization of linked lists, where [] represents None.

Constraints

  • 0 <= n <= 10^5
  • -10^9 <= node.val <= 10^9
  • The input is a singly linked list
  • Use O(1) auxiliary space
  • Node identity must be preserved

Function Signature

def swap_first_two(head):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output