Last Updated: 2023-11-07 11:24:34 Tuesday
-- TOC --
singly-linked list without dummy head,这是我最不喜欢的数据结构,这里给我实现的更现实的链表,包含sentinel的单双链表。
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
题目要求,不能够只是交换value,必须采用交换node的方式。
Example 1:
Input: head = [1,2,3,4]
Output: [2,1,4,3]
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [1]
Output: [1]
Constraints:
- The number of nodes in the list is in the range [0, 100].可能有空链。
- 0 <= Node.val <= 100
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
p = q = head
if p and p.next:
head = p.next
p = q.next = head.next
head.next = q
while p and p.next:
q.next = p.next
p.next = p.next.next
q.next.next = q = p
p = p.next
return head
/**
* 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) noexcept{
ListNode *h { head };
if(head && head->next){
head = h->next;
h->next = head->next;
head->next = h;
ListNode *k1 { h->next };
while(k1 && k1->next){
ListNode *k2 { k1->next };
k1->next = k2->next;
k2->next = k1;
h->next = k2;
h = k1;
k1 = k1->next;
}
}
return head;
}
};
/*class Solution {
public:
ListNode* swapPairs(ListNode* head) noexcept{
ListNode *k, *h;
k = h = head;
if(head && head->next){
head = head->next;
k = h->next = head->next;
head->next = h;
while(k && k->next){
h->next = k->next;
k->next = k->next->next;
h = h->next->next = k;
k = k->next;
}
}
return head;
}
};*/
noexcept
可以用在这里。/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
struct ListNode *k, *h;
k = h = head;
if(head && head->next){
head = head->next;
h->next = k = head->next;
head->next = h;
while(k && k->next){
h->next = k->next;
k->next = k->next->next;
h = h->next->next = k;
k = k->next;
}
}
return head;
}
与C++版本的代码完全一样。
仅用来训练直觉...
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head and head.next:
p = head.next
head.next = self.swapPairs(p.next)
p.next = head
return p
return head
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
if(head && head->next){
struct ListNode *p = head;
head = head->next;
p->next = swapPairs(head->next);
head->next = p;
}
return head;
}
/**
* 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) noexcept{
if(head && head->next){
ListNode *p = head->next;
head->next = swapPairs(p->next);
p->next = head;
return p;
}
return head;
}
};
本文链接:https://cs.pynote.net/ag/leetcode/202310301/
-- EOF --
-- MORE --