【问题标题】:runner technique for weaving elements of linked list链表元素编织的流道技术
【发布时间】:2017-06-07 19:45:42
【问题描述】:

我一直在阅读破解编码面试书。我有点坚持实施用于编织链表元素的跑步者技术。本书内容如下:

“假设你有一个链表a1->a2....->an->b1->b2....bn,你想把它重新排列成a1->b1->a2->b2 ->.....an->bn。你不知道链表的长度,但你只知道它是一个偶数。

你可以让一个指针 p1(快速指针)每两个元素移动一次 p2 每移动一次。当 p1 到达链表的末尾时,p2 将在端点处。然后,将 p1 移回前面并开始“编织”元素。在每次迭代中,p2 选择一个元素并将其插入到 p1 之后。”

现在我在编织开始之前就完全了解了这项技术。所以,在编织之前,我们基本上有(对于 n=4 个元素)

p1      p2
a1->a2->b1->b2

现在,如果我理解正确,我必须移动 p2 指向的元素(即 b1)并将其插入到 p1 之后,换句话说

p1.next = p2

这将导致以下列表 (?)

a1->b1->b2

算法如何从这里开始?

我发现了this 问题,但似乎表明上述步骤会导致

a1->b1->a2->b2

我不明白。我可能在这里遗漏了一些基本的东西。

【问题讨论】:

  • auto p1_next = p1->next; auto p2_next = p2->next; p1->next = p2; p2->next = p1_next; p1 = p1_next; p2 = p2_next;

标签: c++ algorithm linked-list


【解决方案1】:

如果A1 -> A2 -> ... -> AN -> B1 -> B2 ... -> BN 是您的原始链表,则在第一步之后,您将有P1 和P2 分别指向A1 和B1。假设AN 指向null,这可以在第一步中轻松完成。

在每次迭代中,执行以下步骤:

temp1 = P1.next
temp2 = P2.next //1

P2.next = P1.next //2
P1.next = P2 //3

P1 = temp1 
P2 = temp2 //4

以下是第一次迭代中四个位置的变化:

1:

P1                   P2
⬍                    ⬍
A1 → A2 → ... → AN   B1 → B2 ... → BN
     ⬍                    ⬍
    temp1                temp2

2:

P1   .–––––––––––––––.
⬍    ↓               ↑  
A1 → A2 → ... → AN   B1   B2 ... → BN
     ⬍               ⬍    ⬍
    temp1            P2  temp2

3:

P1   .–––––––––––––––.
⬍    ↓               ↑  
A1   A2 → ... → AN   B1   B2 ... → BN   //P2 points on B1
↓    ⬍               ↑    ⬍
↓   temp1            ↑   temp2
·––––––––––––––––––––·

这相当于:

P1                   P2
⬍                    ⬍
A1 → B1 → A2 → ... → AN   B2 ... → BN
          ⬍               ⬍
         temp1           temp2

4:

          P1              P2
          ⬍               ⬍
A1 → B1 → A2 → ... → AN   B2 ... → BN
          ⬍               ⬍
         temp1           temp2

然后您重复相同的操作,直到 P1 到达 AN。

【讨论】:

    【解决方案2】:

    Adam Stelmaszczyk、Igor Tandetnik 和four_lines 提出的算法是正确的;只需注意终止条件,否则终止列表的最终 nullptr 可能会丢失。 four_lines 提倡在编织之前处理这个问题。或者可以在编织过程中通过检查来完成:

    #include "stdafx.h"
    #include <iostream>
    
    namespace
    {
        struct elem
        {
            int val;
            elem* next;
            elem() : val(-1), next(nullptr) {}
        };
    }
    
    int main()
    {
        // setup single-linked list of elements (an array here makes it easy...) 
        const int SIZE = 12; // even number
        elem elems[SIZE];
        for (int x = 0; x < SIZE - 1; ++x) { elems[x].next = &elems[x + 1]; }
        for (int x = 0; x < SIZE; ++x) { elems[x].val = x; }
    
        // divide the list using ptrs pf, ps to point at first part, second part 
        elem* pf = elems;
        elem* ps = elems;
        do
        {
            pf = pf->next; 
            pf = pf->next;
            ps = ps->next;
        } while (pf != nullptr);
        pf = elems; // move pf back to start
    
        // weave first and second parts of list using pf and ps
        do
        {
            // update pf element next-ptr to point at ps, then move pf to its original next element 
            elem* oldpfnext = pf->next;
            pf->next = ps;
            pf = oldpfnext;
            // if ps element is not at end, update ps element next-ptr to point the new location of pf
            elem* oldpsnext = ps->next;
            if (nullptr != ps->next)
            {
                ps->next = pf;
            }
            // now move ps to its original next element (even if null)... 
            ps = oldpsnext;
        } while (nullptr != ps); // ... and stop if we are now at end
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 2021-05-16
      • 2011-06-23
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多