标题泉源
24. 两两交换链表中的节点 - 力扣(LeetCode)
标题形貌
给你一个链表,两两交换此中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的环境下完本钱题(即,只能举行节点交换)。
示例
示例 1:
- <strong>输入:</strong>head = [1,2,3,4]
- <strong>输出:</strong>[2,1,4,3]
复制代码 示例 2:
- <strong>输入:</strong>head = []
- <strong>输出:</strong>[]
复制代码 示例 3:
- <strong>输入:</strong>head = [1]
- <strong>输出:</strong>[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;
- };
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|