0

与えられたリストを 2 つのサブリストに分割します。1 つは前半用、もう 1 つは後半用です。要素の数が奇数の場合、余分な要素はフロント リストに配置する必要があります。したがってFrontBackSplit()、リストにはと{2, 3, 5, 7, 11}の 2 つのリストが生成されます。{2, 3, 5}{7, 11}

コードはこれです。

void FrontBackSplit(Node *head, Node **front, Node **back) {
  if (!head) return;  // Handle empty list
  Node *front_last_node;
  Node *slow = head;
  Node *fast = head;
  while (fast) {
    front_last_node = slow;
    slow = slow->next;
    fast = (fast->next) ? fast->next->next : NULL;
  }
  front_last_node->next = NULL;  // ends the front sublist
  *front = head;
  *back = slow;
}

問題は、最適なランタイムが得られず、期待される出力が得られないことです。

4

2 に答える 2