📌  相关文章
📜  以 Zig-Zag 方式重新排列链表 |组 2

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

以 Zig-Zag 方式重新排列链表 |组 2

给定一个链表,重新排列它,使得转换后的列表应该是 a < b > c < d > e < f .. 其中 a, b, c.. 是链表的连续数据节点。请注意,不允许交换数据。

例子:

Input:  1->2->3->4
Output: 1->3->2->4 

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

方法:
将给定列表转换为锯齿形形式的解决方案在前一篇文章中讨论过。讨论的解决方案通过交换节点的数据来执行转换。当数据包含许多字段时,在许多情况下交换节点的数据可能会很昂贵。在这篇文章中,讨论了通过交换链接执行转换的解决方案。

这个想法是遍历给定的链表并检查当前节点是否保持锯齿形顺序。要检查给定节点是否保持锯齿形顺序,使用变量ind 。如果ind = 0 ,则当前节点的数据应小于其相邻节点的数据;如果ind = 1 ,则当前节点的数据应大于其相邻节点的数据。如果当前节点违反了锯齿形顺序,则交换两个节点的位置。为了完成这一步,维护两个指针prev 和 nextprev存储当前节点的前一个节点, next存储当前节点的新下一个节点。要交换两个节点,执行以下步骤:

  • 使当前节点的下一个节点成为上一个节点的下一个节点。
  • 使当前节点成为其相邻节点的下一个节点。
  • 使当前节点下一个 = 下一个节点。

下面是上述方法的实现:



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 converts the Linked list in
// zigzag fashion
Node* zigZagList(Node* head)
{
    if (head == NULL || head->next == NULL) {
        return head;
    }
 
    // to store new head
    Node* res = NULL;
 
    // to traverse linked list
    Node* curr = head;
 
    // to store previous node of current node
    Node* prev = NULL;
 
    // to store new next node of current node
    Node* next;
 
    // to check if current element should
    // be less than or greater than.
    // ind = 0 --> less than
    // ind = 1 --> greater than
    int ind = 0;
 
    while (curr->next) {
 
        // If elements are not in zigzag fashion
        // swap them.
        if ((ind == 0 && curr->data > curr->next->data)
            || (ind == 1 && curr->data < curr->next->data)) {
 
            if (res == NULL)
                res = curr->next;
 
            // Store new next element of current
            // node
            next = curr->next->next;
 
            // Previous node of current node will
            // now point to next node of current node
            if (prev)
                prev->next = curr->next;
 
            // Change next pointers of both
            // adjacent nodes
            curr->next->next = curr;
            curr->next = next;
 
            // Change previous pointer.
            if (prev)
                prev = prev->next;
            else
                prev = res;
        }
 
        // If already in zig zag form, then move
        // to next element.
        else {
            if (res == NULL) {
                res = curr;
            }
 
            prev = curr;
            curr = curr->next;
        }
 
        // Update info whether next element should
        // be less than or greater than.
        ind = 1 - ind;
    }
 
    return res;
}
 
/* 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;
    }
}
 
/* Driver program to test above function*/
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 \n");
    printList(head);
 
    head = zigZagList(head);
 
    printf("\nZig Zag Linked list \n");
    printList(head);
 
    return 0;
}


Java
// Java program to arrange linked list in
// zigzag fashion
class GFG
{
 
/* Link list Node */
static class Node
{
    int data;
    Node next;
};
 
static Node head;
 
// This function converts the Linked list in
// zigzag fashion
static Node zigZagList(Node head)
{
    if (head == null || head.next == null)
    {
        return head;
    }
 
    // to store new head
    Node res = null;
 
    // to traverse linked list
    Node curr = head;
 
    // to store previous node of current node
    Node prev = null;
 
    // to store new next node of current node
    Node next;
 
    // to check if current element should
    // be less than or greater than.
    // ind = 0 -. less than
    // ind = 1 -. greater than
    int ind = 0;
 
    while (curr.next != null)
    {
 
        // If elements are not in zigzag fashion
        // swap them.
        if ((ind == 0 && curr.data > curr.next.data) ||
            (ind == 1 && curr.data < curr.next.data))
        {
            if (res == null)
                res = curr.next;
 
            // Store new next element of current
            // node
            next = curr.next.next;
 
            // Previous node of current node will
            // now point to next node of current node
            if (prev != null)
                prev.next = curr.next;
 
            // Change next pointers of both
            // adjacent nodes
            curr.next.next = curr;
            curr.next = next;
 
            // Change previous pointer.
            if (prev != null)
                prev = prev.next;
            else
                prev = res;
        }
 
        // If already in zig zag form, then move
        // to next element.
        else
        {
            if (res == null)
            {
                res = curr;
            }
 
            prev = curr;
            curr = curr.next;
        }
 
        // Update info whether next element should
        // be less than or greater than.
        ind = 1 - ind;
    }
    return res;
}
 
/* UTILITY FUNCTIONS */
/* Function to push a Node */
static void push(Node head_ref, int new_data)
{
    /* allocate Node */
    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;
    head = head_ref;
}
 
/* Function to print linked list */
static void printList(Node Node)
{
    while (Node != null)
    {
        System.out.printf("%d->", Node.data);
        Node = Node.next;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    /* Start with the empty list */
    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);
 
    System.out.printf("Given linked list \n");
    printList(head);
 
    head = zigZagList(head);
 
    System.out.printf("\nZig Zag Linked list \n");
    printList(head);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to arrange
# linked list in zigzag fashion
 
class Node:
     
    def __init__(self, data):
        self.data = data
        self.next = None
  
# This function converts the
# Linked list in zigzag fashion
def zigZagList(head):
  
    if head == None or head.next == None: 
        return head
      
    # To store new head
    res = None
 
    # To traverse linked list
    curr = head
 
    # To store previous node of current node
    prev = None
 
    # to check if current element should
    # be less than or greater than.
    # ind = 0 -. less than
    # ind = 1 -. greater than
    ind = 0
 
    while curr.next: 
 
        # If elements are not in
        # zigzag fashion swap them.
        if ((ind == 0 and curr.data > curr.next.data)
            or (ind == 1 and curr.data < curr.next.data)): 
 
            if res == None:
                res = curr.next
 
            # Store new next element of current
            # node
            next = curr.next.next
 
            # Previous node of current node will
            # now point to next node of current node
            if prev:
                prev.next = curr.next
 
            # Change next pointers of
            # both adjacent nodes
            curr.next.next = curr
            curr.next = next
 
            # Change previous pointer.
            if prev:
                prev = prev.next
            else:
                prev = res
         
        # If already in zig zag form,
        # then move to next element.
        else:
            if res == None: 
                res = curr
     
            prev = curr
            curr = curr.next
          
        # Update info whether next element
        # should be less than or greater than.
        ind = 1 - ind
     
    return res
  
# UTILITY FUNCTIONS
# Function to push a Node
def push(head_ref, new_data):
  
    # put in the data
    new_Node = Node(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
    return head_ref
  
# Function to print linked list
def printList(Node):
  
    while Node != None:
        print(Node.data, end = "->")
        Node = Node.next
 
# Driver program to test above function
if __name__ == "__main__":
  
    # Start with the empty list
    head = None
 
    # create a list 4 . 3 . 7 . 8 . 6 . 2 . 1
    # answer should be . 3 7 4 8 2 6 1
    head = push(head, 1)
    head = push(head, 2)
    head = push(head, 6)
    head = push(head, 8)
    head = push(head, 7)
    head = push(head, 3)
    head = push(head, 4)
 
    print("Given linked list")
    printList(head)
 
    head = zigZagList(head)
 
    print("\nZig Zag Linked list")
    printList(head)
         
# This code is contributed by Rituraj Jain


C#
// C# program to arrange linked list in
// zigzag fashion
using System;
     
class GFG
{
 
/* Link list Node */
public class Node
{
    public int data;
    public Node next;
};
 
static Node head;
 
// This function converts the Linked list in
// zigzag fashion
static Node zigZagList(Node head)
{
    if (head == null || head.next == null)
    {
        return head;
    }
 
    // to store new head
    Node res = null;
 
    // to traverse linked list
    Node curr = head;
 
    // to store previous node of current node
    Node prev = null;
 
    // to store new next node of current node
    Node next;
 
    // to check if current element should
    // be less than or greater than.
    // ind = 0 -. less than
    // ind = 1 -. greater than
    int ind = 0;
 
    while (curr.next != null)
    {
 
        // If elements are not in zigzag fashion
        // swap them.
        if ((ind == 0 && curr.data > curr.next.data) ||
            (ind == 1 && curr.data < curr.next.data))
        {
            if (res == null)
                res = curr.next;
 
            // Store new next element of current
            // node
            next = curr.next.next;
 
            // Previous node of current node will
            // now point to next node of current node
            if (prev != null)
                prev.next = curr.next;
 
            // Change next pointers of both
            // adjacent nodes
            curr.next.next = curr;
            curr.next = next;
 
            // Change previous pointer.
            if (prev != null)
                prev = prev.next;
            else
                prev = res;
        }
 
        // If already in zig zag form, then move
        // to next element.
        else
        {
            if (res == null)
            {
                res = curr;
            }
 
            prev = curr;
            curr = curr.next;
        }
 
        // Update info whether next element should
        // be less than or greater than.
        ind = 1 - ind;
    }
    return res;
}
 
/* UTILITY FUNCTIONS */
/* Function to push a Node */
static void push(Node head_ref, int new_data)
{
    /* allocate Node */
    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;
    head = head_ref;
}
 
/* Function to print linked list */
static void printList(Node Node)
{
    while (Node != null)
    {
        Console.Write("{0}->", Node.data);
        Node = Node.next;
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    /* Start with the empty list */
    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);
 
    Console.Write("Given linked list \n");
    printList(head);
 
    head = zigZagList(head);
 
    Console.Write("\nZig Zag Linked list \n");
    printList(head);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Given linked list 
4->3->7->8->6->2->1->
Zig Zag Linked list 
3->7->4->8->2->6->1->

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程