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

单链表拆分问题求助:如何将单链表均分为两个子链表

Splitting a Singly Linked List into Two Equal Halves (splitMerge Function)

Hey there! Let's work through that splitMerge function you're stuck on. Since your input file has an even number of names, splitting the linked list evenly is totally doable with a classic two-pointer trick—here's a step-by-step breakdown and code example to get you sorted.

First, Let's Confirm the Node Structure

I’ll assume you’re using C (super common for data structure assignments), so let’s start with a standard node definition (adjust if you’re using another language):

typedef struct Node {
    char name[50]; // Or use a char* if you're dynamically allocating strings
    struct Node* next;
} Node;

The splitMerge Implementation

The key here is using slow and fast pointers to find the middle of the list without having to count nodes first. Here’s how it works:

  • The fast pointer moves 2 nodes at a time
  • The slow pointer moves 1 node at a time
  • When the fast pointer reaches the end of the list, the slow pointer will be exactly at the midpoint’s previous node (perfect for splitting the list cleanly)
void splitMerge(Node* head, Node** myList1, Node** myList2) {
    // Edge case handling (though your input has even nodes, better safe than sorry)
    if (head == NULL || head->next == NULL) {
        *myList1 = head;
        *myList2 = NULL;
        return;
    }

    Node* slow = head;
    Node* fast = head->next;

    // Traverse until fast hits the end of the list
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
    }

    // Split the list into two halves
    *myList1 = head;          // First half starts at the original head
    *myList2 = slow->next;    // Second half starts right after the slow pointer
    slow->next = NULL;        // Cut the link between the two halves—critical!
}

How This Works for Even-Length Lists

Let’s say your linked list is: Alice → Bob → Charlie → Dave (4 nodes, even):

  1. Fast starts at Bob, slow starts at Alice
  2. First iteration: slow moves to Bob, fast moves to Dave
  3. Next iteration: fast->next is NULL, so we stop
  4. Slow is at Bob, so myList1 is Alice→Bob, myList2 is Charlie→Dave, and we set Bob->next = NULL to disconnect them

Testing with Your Existing Functions

Pair this with your traverse function to verify it works:

void traverse(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%s ", current->name);
        current = current->next;
    }
    printf("\n");
}

// Example usage in main
int main() {
    Node* fullList = readFile("your_input.txt"); // Your existing readFile function

    Node* list1 = NULL;
    Node* list2 = NULL;
    splitMerge(fullList, &list1, &list2);

    printf("First half: ");
    traverse(list1);
    printf("Second half: ");
    traverse(list2);

    // Don't forget to free memory later!
    return 0;
}

Quick Notes to Avoid Pitfalls

  • Make sure you pass pointers to pointers (Node**) for myList1 and myList2—this lets the function modify the original list head pointers outside the function.
  • Always set slow->next = NULL—if you skip this, your two sublists will still be connected at the tail of the first half.
  • This logic works for odd-length lists too (the first half will be one node longer), but since your input is even, you don’t have to worry about that edge case.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:23:46