【问题标题】:Recursive solution for truncating a linked list用于截断链表的递归解决方案
【发布时间】:2021-12-01 12:25:28
【问题描述】:
public class Solution {

  /**
   * Return a list containing, at most, the first numNodes nodes in the list starting with head. If
   * numNodes is larger than the length of the list, then the entire list will be returned.
   * 
   * Examples:
   * 
   * <pre>
   * [11, 12, 13], numNodes = 2
   * returns [11, 12]
   * 
   * [11, 12, 13], numNodes = 0
   * returns []
   * 
   * [11, 12, 13], numNodes = 100
   * returns [11, 12, 13]
   * 
   * [], numNodes = 5
   * returns []
   * </pre>
   * 
   * @param head - Head of the list to truncate
   * @param numNodes - Maximum number of nodes in the resulting list
   * @return head of the truncated list
   */
  public static ListNode truncate(ListNode head, int numNodes) {
    // just return null
    if (head == null) {
      return null;
    }
    // Recursion head.next
    if (head.next != null) {
      head.next = truncate(head.next, numNodes--);
    }
    
    if (numNodes <= 0) {
      return head.next;
    } else {
      return head;
    }
  }

如您所见,这段代码的目的是截断超过 numNodes 的每个节点。除了通过长度为 4 的列表并且我需要返回前 3 个节点(numNodes = 3)的情况之外,我还通过了所有测试。如果有人可以帮助我,那将不胜感激。我不知道我做错了什么。

这是 ListNode 类

/**
 * Simple Singly-linked-list node.
 */
public class ListNode {

  public int val;
  public ListNode next;

  public ListNode(int val, ListNode next) {
    this.val = val;
    this.next = next;
  }

  public ListNode(int val) {
    this.val = val;
    this.next = null;
  }

  public ListNode() {
    this.val = 0;
    this.next = null;
  }
}

这是我唯一没有通过的测试

testTruncateLengthFourListToThree() 预期:[10,11,12] 实际:[10,11,12,13]

解决方案必须是递归的。

【问题讨论】:

    标签: java recursion linked-list truncate


    【解决方案1】:

    试试这个。

    record ListNode(int val, ListNode next) {}
    
    public static ListNode truncate(ListNode head, int numNodes) {
        if (head == null || numNodes <= 0)
            return null;
        else
            return new ListNode(head.val, truncate(head.next, numNodes - 1));
    }
    
    public static String toString(ListNode head) {
        List<String> list = new ArrayList<>();
        for ( ; head != null; head = head.next)
            list.add("" + head.val);
        return list.toString();
    }
    
    public static void main(String[] args) {
        ListNode list = new ListNode(1, new ListNode(2, new ListNode(3, null)));
        System.out.println("list = " + toString(list));
        System.out.println("truncate(list, 3) = " + toString(truncate(list, 3)));
        System.out.println("truncate(list, 2) = " + toString(truncate(list, 2)));
        System.out.println("truncate(list, 1) = " + toString(truncate(list, 1)));
        System.out.println("truncate(list, 0) = " + toString(truncate(list, 0)));
    }
    

    输出:

    list = [1, 2, 3]
    truncate(list, 3) = [1, 2, 3]
    truncate(list, 2) = [1, 2]
    truncate(list, 1) = [1]
    truncate(list, 0) = []
    

    【讨论】:

      【解决方案2】:

      也许您可以在递归调用截断时使用前缀增量而不是后缀,从 truncate(head.next, numNodes--) 更改为 truncate(head.next, --numNodes) 以便在递归之前递减 numNodes .

      【讨论】:

      • 也许你可以 - 你能还是不能?你的答案是什么?
      • “也许你可以”我的意思是“你可以”。对不起我的英语不好,谢谢。
      • 好的明白了。
      猜你喜欢
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 2021-11-03
      相关资源
      最近更新 更多