给定一个单向链表,编写函数交换相邻 两个元素
输入: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
输出: 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> 7
输入: 1 -> 2 -> 3 -> 4 -> 5 -> 6
输出: 2 -> 1 -> 4 -> 3 -> 6 -> 5
通过观察发现:当输入的与元素个数是单数的时候,最后一位不参与交换。
方法1(迭代)
从头节点开始遍历列表,遍历过程中使用每个节点的下一个节点和当前节点的数据进行交换。
时间复杂度:O(n)
实现过程:
c语言
/*C Program to pairwise swap elements in a given linked list*/#include#include
/*A linked list node*/
structNode {intdata;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)
{inttemp;
temp= *a;*a = *b;*b =temp;
}/*Funtion to add a node at the beginning of Linked List*/
void push(struct Node** head_ref, intnew_data)
{/*allocate node*/
struct Node* new_node = (struct Node*)malloc(sizeof(structNode));/*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("");
}/*driver program to test above function*/
intmain() {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");
printList(start);
pairWiseSwap(start);
printf("Linked list after calling pairwise swap function");
printList(start);return 0;
}
结果
java代码:
//Java program to pairwise swap elements of a linked list
classLinkedList {
Node head;//head of list
/*Linked list Node*/
classNode {intdata;
Node next;
Node(intd)
{
data=d;
next= null;
}
}voidpairWiseSwap()
{
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(intnew_data)
{/*1 & 2: Allocate the Node &
Put in the data*/Node new_node= newNode(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*/
voidprintList()
{
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 voidmain(String args[])
{
LinkedList llist= newLinkedList();/*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
usingSystem;classLinkedList {
Node head;//head of list
/*Linked list Node*/
public classNode {public intdata;publicNode next;public Node(intd)
{
data=d;
next= null;
}
}voidpairWiseSwap()
{
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(intnew_data)
{/*1 & 2: Allocate the Node &
Put in the data*/Node new_node= newNode(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*/
voidprintList()
{
Node temp=head;while (temp != null) {
Console.Write(temp.data+ " ");
temp=temp.next;
}
Console.WriteLine();
}/*Driver program to test above functions*/
public static voidMain(String[] args)
{
LinkedList llist= newLinkedList();/*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);
}
}
以上代码只是交换数据域部分的指针,如果一个节点含有很多字段,将会产生频繁的交换动作。