Leetcode 24 两两交换链表节点
看到题目最开始没理解题的意思,特别是奇数个链表时的情况。后来想明白很快做出了。要注意空链表和只有一个节点的情况。
/**
* 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* dummyHead = new ListNode(0, head);
ListNode* cur = dummyHead;
while(cur -> next != nullptr && cur -> next -> next != nullptr){
ListNode* newNode = cur -> next;
ListNode* new2 = cur -> next -> next -> next;
cur -> next = cur -> next -> next;
cur -> next -> next = newNode;
cur = cur -> next -> next;
cur -> next = new2;
}
return dummyHead -> next;
}
};
Leetcode 19 删除倒数第n个节点
首先暴力解法,先遍历节点得到链表大小,然后再遍历size-n,进行删除。
/**
* 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* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0, head);
ListNode* cur = dummyHead;
int _size = 0;
while(cur -> next != nullptr){
cur = cur -> next;
_size++;
}
int index = _size - n;
cur = dummyHead;
while(index--){
cur = cur -> next;
}
ListNode* deleteNode = cur -> next;
cur -> next = cur -> next -> next;
delete deleteNode;
deleteNode = nullptr;
return dummyHead -> next;
}
};
看了官解之后,使用双指针法,首先fast指向n+1,slow指向头节点,fast移动到末尾时,slow刚好移动到倒数第n+1个节点。:
/**
* 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* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0, head);
ListNode* fast = dummyHead;
ListNode* slow = dummyHead;
int i = 0;
while(fast -> next != nullptr){
if(i < n){
fast = fast -> next;
}
else{
fast = fast -> next;
slow = slow -> next;
}
i++;
}
ListNode* deleteNode = slow -> next;
slow -> next = slow -> next -> next;
delete deleteNode;
deleteNode = nullptr;
return dummyHead -> next;
}
};
Leetcode 面试题02.07 链表相交
先求两个链表的大小,然后让两个链表都从相同长度开始遍历。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int _sizeA = 0;
int _sizeB = 0;
ListNode* curA = headA;
ListNode* curB = headB;
while(curA != nullptr){
curA = curA -> next;
_sizeA++;
}
while(curB != nullptr){
curB = curB -> next;
_sizeB++;
}
int gap = abs(_sizeA - _sizeB);
curA = headA;
curB = headB;
if(_sizeA < _sizeB){
swap(_sizeA, _sizeB);
swap(curA, curB);
}
while(gap--){
curA = curA -> next;
}
while(curA != nullptr){
if(curA == curB)
return curA;
curA = curA -> next;
curB = curB -> next;
}
return NULL;
}
};
Leetcode 142 环形链表2
这道题很巧,先让一个fast每次走两步,再让一个slow每次走一步,相遇之后再让一个指针从相遇点和头节点同时每次走一步,再次相遇就是环形的入口。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
while(fast != nullptr && fast -> next != nullptr){
slow = slow -> next;
fast = fast -> next -> next;
if(slow == fast){
ListNode* cur = head;
while(cur != fast){
cur = cur -> next;
fast = fast -> next;
}
return cur;
}
}
return NULL;
}
};