📜  以之字形方式重新排列链表的 C++ 程序

📅  最后修改于: 2022-05-13 01:57:45.285000             🧑  作者: Mango

以之字形方式重新排列链表的 C++ 程序

给定一个链表,重新排列它,使转换后的链表的形式为 a < b > c < d > e < f ... 其中 a, b, c ... 是链表的连续数据节点。

例子:

Input:  1->2->3->4
Output: 1->3->2->4 
Explanation: 1 and 3 should come first before 2 and 4 in
             zig-zag fashion, So resultant linked-list 
             will be 1->3->2->4. 

Input:  11->15->20->5->10
Output: 11->20->5->15->10 

我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。

一个简单的方法是使用合并排序对链表进行排序,然后交换交替,但这需要 O(n Log n) 时间复杂度。这里 n 是链表中元素的数量。

一种需要 O(n) 时间的有效方法是,使用类似于冒泡排序的单次扫描,然后维护一个标志来表示我们当前的顺序 ()。如果当前的两个元素不是该顺序,则交换这些元素,否则不交换。有关交换顺序的详细说明,请参阅此处。

C++
// C++ program to arrange linked
// list in zigzag fashion
#include 
using namespace std;
  
// Link list Node 
struct Node 
{
    int data;
    struct Node* next;
};
  
// This function distributes the
// Node in zigzag fashion
void zigZagList(Node* head)
{
    // If flag is true, then next
    // node should be greater
    // in the desired output.
    bool flag = true;
  
    // Traverse linked list starting 
    // from head.
    Node* current = head;
    while (current->next != NULL) 
    {
        // "<" relation expected 
        if (flag) 
        {
            /* If we have a situation like 
               A > B > C where A, B and C 
               are consecutive Nodes in list 
               we get A > B < C by swapping B
               and C */
            if (current->data > 
                current->next->data)
                swap(current->data, 
                     current->next->data);
        }
        // ">" relation expected 
        else 
        {
            /* If we have a situation like 
               A < B < C where A, B and C  
               are consecutive Nodes in list we
               get A < C > B by swapping B and C */
            if (current->data < 
                current->next->data)
                swap(current->data, 
                     current->next->data);
        }
  
        current = current->next;
  
        // flip flag for reverse checking 
        flag = !flag; 
    }
}
  
// UTILITY FUNCTIONS 
// Function to push a Node 
void push(Node** head_ref, 
          int new_data)
{
    // Allocate Node 
    struct Node* new_Node = new Node;
  
    // Put in the data  
    new_Node->data = new_data;
  
    // Link the old list off the 
    // new Node 
    new_Node->next = (*head_ref);
  
    // Move the head to point to 
    // the new Node 
    (*head_ref) = new_Node;
}
  
// Function to print linked list 
void printList(struct Node* Node)
{
    while (Node != NULL) 
    {
        printf("%d->", Node->data);
        Node = Node->next;
    }
    printf("NULL");
}
  
// Driver code
int main(void)
{
    // Start with the empty list 
    struct Node* head = NULL;
  
    // create a list 4 -> 3 -> 7 -> 
    // 8 -> 6 -> 2 -> 1 
    // answer should be -> 3  7  4  
    // 8  2  6  1
    push(&head, 1);
    push(&head, 2);
    push(&head, 6);
    push(&head, 8);
    push(&head, 7);
    push(&head, 3);
    push(&head, 4);
  
    printf("Given linked list ");
    printList(head);
  
    zigZagList(head);
  
    printf("Zig Zag Linked list ");
    printList(head);
  
    return (0);
}


输出:

Given linked list 
4->3->7->8->6->2->1->NULL
Zig Zag Linked list 
3->7->4->8->2->6->1->NULL

有关详细信息,请参阅有关以 Zig-Zag 方式重新排列链接列表的完整文章!