交换单链表相邻的两个节点

给定一个链表,对每两个相邻的结点作交换并返回头节点。

例如:
给定 1->2->3->4,你应该返回 2->1->4->3

你的算法应该只使用额外的常数空间。不要修改列表中的值,只有节点本身可以​​更改。

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def swapPairs(self, head):
        pre, pre.next = self, head
        while pre.next and pre.next.next:
            a = pre.next
            b = a.next
            pre.next, b.next, a.next = b, a, b.next
            pre = a
        return self.next


单链表交换两个相邻结点可以通过两种常见的方式来实现,分别是交换节点的数据域和交换节点本身。 #### 方法一:交换数据域 这种方法相对简单,只需要交换两个相邻节点的数据域,而不需要改变节点的指针连接。以下是用 C 语言实现的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct Node { int data; struct Node* next; }; // 交换相邻节点的数据域 void swapAdjacentData(struct Node* head) { struct Node* current = head; while (current != NULL && current->next != NULL) { // 交换当前节点和下一个节点的数据 int temp = current->data; current->data = current->next->data; current->next->data = temp; // 移动到下一对相邻节点 current = current->next->next; } } // 插入新节点到链表头部 void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } // 打印链表 void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } int main() { struct Node* head = NULL; // 插入节点 1->2->3->4->5 push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); push(&head, 1); printf("Original list: "); printList(head); // 交换相邻节点的数据域 swapAdjacentData(head); printf("List after swapping adjacent nodes: "); printList(head); return 0; } ``` #### 方法二:交换节点本身 这种方法需要改变节点的指针连接,实现起来相对复杂一些,但在某些情况下(如节点数据域较大)更高效。以下是用 C 语言实现的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct Node { int data; struct Node* next; }; // 交换相邻节点本身 struct Node* swapAdjacentNodes(struct Node* head) { // 如果链表为空或只有一个节点,直接返回 if (head == NULL || head->next == NULL) { return head; } // 初始化前一个节点和当前节点 struct Node* prev = NULL; struct Node* curr = head; head = curr->next; // 新的头节点 while (curr != NULL && curr->next != NULL) { struct Node* next = curr->next; // 交换相邻节点 curr->next = next->next; next->next = curr; if (prev != NULL) { prev->next = next; } // 更新前一个节点和当前节点 prev = curr; curr = curr->next; } return head; } // 插入新节点到链表头部 void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } // 打印链表 void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } int main() { struct Node* head = NULL; // 插入节点 1->2->3->4->5 push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); push(&head, 1); printf("Original list: "); printList(head); // 交换相邻节点本身 head = swapAdjacentNodes(head); printf("List after swapping adjacent nodes: "); printList(head); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值