Is It a Prefix
Return True if the values of list a appear at the very start of list b, in order. An empty a is a prefix of every list, and a list is a prefix of itself.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
a [1,2]
b [1,2,3]
returns true
b starts with 1, 2.
Example 2
a [2,3]
b [1,2,3]
returns false
b starts with 1, not 2.
Constraints
0 <= nodes in each list <= 50-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Linked lists problems
- Is the List SortedBeginner
- Kth Smallest ValueBeginner
- Largest Adjacent Pair SumBeginner
- Largest ValueBeginner
- Last Node's ValueBeginner
- Linked List to TextBeginner
- Longest Run of Equal ValuesBeginner
- One Bubble PassBeginner