LeetCode - 24 两两交换链表中的节点
题目来源
24. 两两交换链表中的节点 - 力扣(LeetCode)
题目描述
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
示例
示例 1:
输入:head = [1,2,3,4] 输出:[2,1,4,3]
示例 2:
输入:head = [] 输出:[]
示例 3:
输入:head = [1] 输出:[1]
提示
- 链表中节点的数目在范围
[0, 100]
内 0 <= Node.val <= 100
题目解析
本题主要考察数据结构。
对于输入的链表,我们可以为其定义一个虚拟头节点 dummy_head,比如示例1,进行如下逻辑
C源码实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
struct ListNode* dummy_head = (struct ListNode*)malloc(sizeof(struct ListNode));
dummy_head->val = 0;
dummy_head->next = head;
struct ListNode* pre = dummy_head;
struct ListNode* cur = pre->next;
while (cur != NULL && cur->next != NULL) { // 由于要交换cur和cur.next两个节点,因此二者不能为null
struct ListNode* nxt = cur->next;
cur->next = nxt->next;
nxt->next = cur;
pre->next = nxt;
pre = cur;
cur = pre->next;
}
return dummy_head->next;
}
C++源码实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummy_head = new ListNode(0, head);
ListNode* pre = dummy_head;
ListNode* cur = pre->next;
while (cur != nullptr && cur->next != nullptr) { // 由于要交换cur和cur.next两个节点,因此二者不能为null
ListNode* nxt = cur->next;
cur->next = nxt->next;
nxt->next = cur;
pre->next = nxt;
pre = cur;
cur = pre->next;
}
return dummy_head->next;
}
};
Java源码实现
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy_head = new ListNode(0, head);
ListNode pre = dummy_head;
ListNode cur = pre.next;
while (cur != null && cur.next != null) { // 由于要交换cur和cur.next两个节点,因此二者不能为null
ListNode nxt = cur.next;
cur.next = nxt.next;
nxt.next = cur;
pre.next = nxt;
pre = cur;
cur = pre.next;
}
return dummy_head.next;
}
}
Python源码实现
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
"""
dummy_head = ListNode(0, head)
pre = dummy_head
cur = pre.next
while cur and cur.next: # 由于要交换cur和cur.next两个节点,因此二者不能为null
nxt = cur.next
cur.next = nxt.next
nxt.next = cur
pre.next = nxt
pre = cur
cur = pre.next
return dummy_head.next
JavaScript源码实现
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var swapPairs = function (head) {
const dummy_head = new ListNode(0, head);
let pre = dummy_head;
let cur = pre.next;
while (cur != null && cur.next != null) { // 由于要交换cur和cur.next两个节点,因此二者不能为null
const nxt = cur.next;
cur.next = nxt.next;
nxt.next = cur;
pre.next = nxt;
pre = cur;
cur = pre.next;
}
return dummy_head.next;
};