You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现对链表应用UnaryTransformer并返回修改后的链表?

Implementing transformAll for Linked Lists

Got it, let's break down how to solve this problem. The goal is to build the transformAll function that takes a linked list head (holding Q type objects) and a UnaryTransformer, applies the transformer to every element in the list, and returns the modified result. We don't need to worry about the transformer's specific logic—just focus on properly traversing and updating the linked list.

Step 1: Define Basic Structures (if not provided)

First, let's assume we're working with a standard singly linked list node and the UnaryTransformer interface. Adjust the syntax based on your programming language, but here's a common Java-style example:

// Linked list node structure
class Node<Q> {
    Q data;
    Node<Q> next;
    
    Node(Q data) {
        this.data = data;
        this.next = null;
    }
}

// UnaryTransformer interface as described in the problem
interface UnaryTransformer<Q> {
    Q transform(Q input);
}

Step 2: Implement transformAll (Two Common Approaches)

You have two main options here—modify the list in-place or create a new list to preserve the original.

Option 1: In-Place Modification (Space Efficient)

This version modifies the original list's nodes directly, using O(1) extra space:

public static <Q> Node<Q> transformAll(Node<Q> head, UnaryTransformer<Q> transformer) {
    // Handle empty list edge case first
    if (head == null) {
        return null;
    }
    
    // Traverse every node in the list
    Node<Q> current = head;
    while (current != null) {
        // Apply the transformer to the current node's data
        current.data = transformer.transform(current.data);
        // Move to the next node
        current = current.next;
    }
    
    // Return the modified list's head (same as original since we updated in-place)
    return head;
}

Option 2: Create a New Linked List (Non-Destructive)

If you need to keep the original list intact, this version builds a new list with transformed elements:

public static <Q> Node<Q> transformAll(Node<Q> head, UnaryTransformer<Q> transformer) {
    if (head == null) {
        return null;
    }
    
    // Create the new list's head with transformed data
    Node<Q> newHead = new Node<>(transformer.transform(head.data));
    Node<Q> currentNew = newHead;
    Node<Q> currentOld = head.next;
    
    // Iterate through the original list, building new nodes
    while (currentOld != null) {
        Node<Q> newNode = new Node<>(transformer.transform(currentOld.data));
        currentNew.next = newNode;
        currentNew = newNode;
        currentOld = currentOld.next;
    }
    
    // Return the head of the transformed new list
    return newHead;
}

Key Takeaways

  • Edge Case Handling: Always check for a null input head first to avoid null pointer exceptions.
  • Iteration Over Recursion: Iterative traversal is preferred here because recursion can cause stack overflow with very long linked lists.
  • Transformer Agnosticism: We only need to call transformer.transform() on each node's data—we don't care what the transformer actually does, just that it accepts and returns a Q type.

内容的提问来源于stack exchange,提问作者009

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:50:12