RMS Careers Logo
RMS Careers
Public Starter Sheet~4 hours
0 / 2 solved

Linked Lists & Recursion Foundations

Understand linear data structures at memory reference level. Learn fast/slow Floyd pointer techniques, list reversals, and sentinel dummy nodes.

easyIterative Pointer Reversal

Reverse Linked List

Given the head of a singly linked list, reverse the list, and return the reversed list.

Examples

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Constraints:
  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000
Target Complexity:
Time: O(N)
Space: O(1)
Starter Code Template
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reverseList(self, head: ListNode | None) -> ListNode | None:
        prev = None
        curr = head
        while curr:
            nxt = curr.next
            curr.next = prev
            prev = curr
            curr = nxt
        return prev
Anonymous Session: Local browser storage only
easyFast & Slow Pointers

Linked List Cycle (Floyd)

Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Return true if there is a cycle in the linked list. Otherwise, return false.

Examples

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Constraints:
  • The number of the nodes in the list is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
Target Complexity:
Time: O(N)
Space: O(1)
Starter Code Template
class Solution:
    def hasCycle(self, head: ListNode | None) -> bool:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False
Anonymous Session: Local browser storage only
Linked Lists & Recursion Foundations — Starter DSA Practice | RMS Careers