不带温度的链表反向

发布于 2024-12-26 04:24:57 字数 526 浏览 0 评论 0原文

有没有办法在 C 中不使用临时变量来反转链表? 提前致谢。

著名的方法:

Element *reverse(Element *head)
{
    Element *previous = NULL;

    while (head != NULL) {
        // Keep next node since we trash
        // the next pointer.
        Element *next = head->next;

        // Switch the next pointer
        // to point backwards.
        head->next = previous;

        // Move both pointers forward.
        previous = head;
        head = next;
    }

    return previous;
}

使用临时变量

Saurabh

Is there any way to reverse linked list without using temp variable in C?
Thanks in advance.

the famous approach:

Element *reverse(Element *head)
{
    Element *previous = NULL;

    while (head != NULL) {
        // Keep next node since we trash
        // the next pointer.
        Element *next = head->next;

        // Switch the next pointer
        // to point backwards.
        head->next = previous;

        // Move both pointers forward.
        previous = head;
        head = next;
    }

    return previous;
}

uses temp variable

Saurabh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

知足的幸福 2025-01-02 04:24:57

请注意,您的 temp 使用实际上生成了两个 swap() 调用,并且可以替换为:

swap(head->next,previous);
swap(previous,head);

您可以使用 xor 在没有临时值的情况下进行交换,称为 异或交换

Note that your temp usage is actually generating two swap() calls, and can be replaced with:

swap(head->next,previous);
swap(previous,head);

You can swap without temps using xor, it is called xor swap.

夏末 2025-01-02 04:24:57

在指针上使用XOR-swaps来伪造XOR-linked-list。

实现留给读者作为练习。

Use XOR-swaps on the pointers to fake an XOR-linked-list.

Implementation is left to the reader as an exercise.

来日方长 2025-01-02 04:24:57

递归方法:

Element *reverse(Element *head, Element **first)
{
    if (head->next == NULL)
    {
        *first = head;
        return head;
    }


     Element* NextElement= reverse  (head->next, first);
     NextElement->next = head;
     head->next = null;

     return head;
}

调用递归函数:

   Element *revLinkListHead;
   reverse(head, &revLinkListHead);

Recursive approach :

Element *reverse(Element *head, Element **first)
{
    if (head->next == NULL)
    {
        *first = head;
        return head;
    }


     Element* NextElement= reverse  (head->next, first);
     NextElement->next = head;
     head->next = null;

     return head;
}

Call for recursive function:

   Element *revLinkListHead;
   reverse(head, &revLinkListHead);
只是在用心讲痛 2025-01-02 04:24:57

如果有人仍然感兴趣,这里是根本不使用新变量的解决方案,除了在递归调用中传递的变量之外。

public static List invert(List l) {
    invert(l.next, l, l);
    l = l.next;
    breakCycle(l, l);
    return l;
}

private static void invert(List l, List toBeNext, List first) {
    if(l.next == null) {
        l.next = toBeNext;
        first.next = l;
    } else {
        invert(l.next, l, first);
        l.next = toBeNext;
    }
}

private static void breakCycle(List l, List first) {
    if(l.next == first) {
        l.next = null;
    } else {
        breakCycle(l.next, first);
    }
}

这个想法如下:首先我们递归地运行 invert 函数,并实现它,以便当它到达最后一个元素时,它将它分配为当前头的下一个元素(参数 first)。执行后,我们将得到一个反向列表,但会循环,因此当前的 head.next 将指向反向列表的头部。我们将 head 重新分配给它的下一个元素(反向列表的实际头),我们要做的最后一件事就是打破循环。所以我们调用 breakCycle 来递归地完成这项工作!

If someone is still interested, here is the solution that uses no new variables at all, except for those passed in recursive call.

public static List invert(List l) {
    invert(l.next, l, l);
    l = l.next;
    breakCycle(l, l);
    return l;
}

private static void invert(List l, List toBeNext, List first) {
    if(l.next == null) {
        l.next = toBeNext;
        first.next = l;
    } else {
        invert(l.next, l, first);
        l.next = toBeNext;
    }
}

private static void breakCycle(List l, List first) {
    if(l.next == first) {
        l.next = null;
    } else {
        breakCycle(l.next, first);
    }
}

The idea is the following: first we run invert function recursively, and implement it so that when it reaches the last element it assigns it as a next element of current head (parameter first). After we executed it, we will have a reversed list but cycled, so the current head.next will point at the head of the reversed list. We reassign head to its next element (the actual head of the reversed list), and the last thing we have to do is to break the cycle. So we call breakCycle which does the job recursively!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文