數據結構:單向鏈表系列6--交換相鄰兩個節點1(交換數據域)

給定一個單向鏈表,編寫函數交換相鄰 兩個元素html

輸入: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7java

輸出: 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> 7node

 

輸入: 1 -> 2 -> 3 -> 4 -> 5 -> 6算法

輸出: 2 -> 1 -> 4 -> 3 -> 6 -> 5c#

經過觀察發現:當輸入的與元素個數是單數的時候,最後一位不參與交換。 數據結構

方法1(迭代)

從頭節點開始遍歷列表,遍歷過程當中使用每一個節點的下一個節點和當前節點的數據進行交換。函數

時間複雜度:O(n)spa

實現過程:3d

c語言指針

/* C Program to pairwise swap elements in a given linked list */
#include <stdio.h>
#include <stdlib.h>

/* A linked list node*/
struct Node {
    int data;
    struct Node* next;
};


/* Function to swap two integers at addresses a and b */
void swap(int *a, int *b);

/* Function to pairwise swap elements of a linked list */
void pairWiseSwap(struct Node* head)
{
    struct Node* temp = head;

    /* Traverse further only if there are at-least two nodes left */
    while( temp != NULL && temp->next != NULL)
    {
        /* Swap data of node with its next node's data */
        swap(&temp->data, &temp->next->data);

        /* Move temp by 2 for the next pair*/
        temp = temp->next->next;
    }
}

/* Utility functions */
/* function to swap two integers */
void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}


/* Funtion to add a node at the beginning of Linked List*/
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct 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 nodes in a given linked list */
void printList(struct Node* node)
{
    while( node != NULL )
    {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}


/* driver program to test above function */
int main() {

    struct Node* start = NULL;

    /*
        the constructed linked list is:
        1 -> 2 -> 3 -> 4 -> 5
     */
    push(&start ,5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1)    ;

    printf("Linked list before calling pairwise swap function\n");

    printList(start);

    pairWiseSwap(start);

    printf("Linked list after calling pairwise swap function\n");

    printList(start);

    return 0;
}

結果

 

 java代碼:

// Java program to pairwise swap elements of a linked list 
class LinkedList { 
    Node head; // head of list 
  
    /* Linked list Node*/
    class Node { 
        int data; 
        Node next; 
        Node(int d) 
        { 
            data = d; 
            next = null; 
        } 
    } 
  
    void pairWiseSwap() 
    { 
        Node temp = head; 
  
        /* Traverse only till there are atleast 2 nodes left */
        while (temp != null && temp.next != null) { 
  
            /* Swap the data */
            int k = temp.data; 
            temp.data = temp.next.data; 
            temp.next.data = k; 
            temp = temp.next.next; 
        } 
    } 
  
    /* Utility functions */
  
    /* Inserts a new Node at front of the list. */
    public void push(int new_data) 
    { 
        /* 1 & 2: Allocate the Node & 
                  Put in the data*/
        Node new_node = new Node(new_data); 
  
        /* 3. Make next of new Node as head */
        new_node.next = head; 
  
        /* 4. Move the head to point to new Node */
        head = new_node; 
    } 
  
    /* Function to print linked list */
    void printList() 
    { 
        Node temp = head; 
        while (temp != null) { 
            System.out.print(temp.data + " "); 
            temp = temp.next; 
        } 
        System.out.println(); 
    } 
  
    /* Driver program to test above functions */
    public static void main(String args[]) 
    { 
        LinkedList llist = new LinkedList(); 
  
        /* Created Linked List 1->2->3->4->5 */
        llist.push(5); 
        llist.push(4); 
        llist.push(3); 
        llist.push(2); 
        llist.push(1); 
  
        System.out.println("Linked List before calling pairWiseSwap() "); 
        llist.printList(); 
  
        llist.pairWiseSwap(); 
  
        System.out.println("Linked List after calling pairWiseSwap() "); 
        llist.printList(); 
    } 
} 
/* This code is contributed by Rajat Mishra */

c#代碼

// C# program to pairwise swap elements of a linked list 
using System; 
class LinkedList { 
    Node head; // head of list 
  
    /* Linked list Node*/
    public class Node { 
        public int data; 
        public Node next; 
        public Node(int d) 
        { 
            data = d; 
            next = null; 
        } 
    } 
  
    void pairWiseSwap() 
    { 
        Node temp = head; 
  
        /* Traverse only till there are atleast 2 nodes left */
        while (temp != null && temp.next != null) { 
  
            /* Swap the data */
            int k = temp.data; 
            temp.data = temp.next.data; 
            temp.next.data = k; 
            temp = temp.next.next; 
        } 
    } 
  
    /* Utility functions */
  
    /* Inserts a new Node at front of the list. */
    public void push(int new_data) 
    { 
        /* 1 & 2: Allocate the Node &  
                Put in the data*/
        Node new_node = new Node(new_data); 
  
        /* 3. Make next of new Node as head */
        new_node.next = head; 
  
        /* 4. Move the head to point to new Node */
        head = new_node; 
    } 
  
    /* Function to print linked list */
    void printList() 
    { 
        Node temp = head; 
        while (temp != null) { 
            Console.Write(temp.data + " "); 
            temp = temp.next; 
        } 
        Console.WriteLine(); 
    } 
  
    /* Driver program to test above functions */
    public static void Main(String[] args) 
    { 
        LinkedList llist = new LinkedList(); 
  
        /* Created Linked List 1->2->3->4->5 */
        llist.push(5); 
        llist.push(4); 
        llist.push(3); 
        llist.push(2); 
        llist.push(1); 
  
        Console.WriteLine("Linked List before calling pairWiseSwap() "); 
        llist.printList(); 
  
        llist.pairWiseSwap(); 
  
        Console.WriteLine("Linked List after calling pairWiseSwap() "); 
        llist.printList(); 
    } 
} 
// This code is contributed by Arnab Kundu 

 

 方法2(遞歸)

 若是鏈表中含有兩個以上節點,先交換前兩個節點,而後遞歸調用剩下的節點

時間複雜度:O(n)

算法主要部分代碼

/* Recursive function to pairwise swap elements 
   of a linked list */
void pairWiseSwap(struct node* head) 
{ 
    /* There must be at-least two nodes in the list */
    if (head != NULL && head->next != NULL) { 
  
        /* Swap the node's data with data of next node */
        swap(&head->data, &head->next->data); 
  
        /* Call pairWiseSwap() for rest of the list */
        pairWiseSwap(head->next->next); 
    } 
} 

以上代碼只是交換數據域部分的指針,若是一個節點含有不少字段,將會產生頻繁的交換動做。

其餘方法:數據結構:單向鏈表系列7--交換相鄰兩個節點2

文章來源:https://www.geeksforgeeks.org/pairwise-swap-elements-of-a-given-linked-list/

相關文章
相關標籤/搜索