判断链表是否有环
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow=head;
ListNode* fast = head;
if(head==NULL){
return false;
}
while(fast&&fast->next){
fast=fast->next->next;
slow=slow->next;
if(fast==slow){
return true;
}
}
return false;
}
};
合并链表(2个)
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
if(!pHead1){
return pHead2;
}
if(!pHead2){
return pHead1;
}
if(pHead1->val <= pHead2->val){
pHead1->next = Merge(pHead1->next, pHead2);
return pHead1;
}else{
pHead2->next = Merge(pHead1,pHead2->next);
return pHead2;
}
}
反转链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* p=nullptr;
ListNode* cur = head;
while(cur){
ListNode* n = cur->next;
cur->next=p;
p=cur;
cur = n;
}
return p;
}
};
删除链表倒数第n个结点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* slow = head;
ListNode* fast = head;
ListNode* tem = slow;
while(fast && n--){
fast = fast->next;
}
if(!fast){
return tem->next;
}
while(fast){
tem = slow;
slow = slow->next;
fast = fast->next;
}
tem->next = slow->next;
return head;
}
};
复杂链表复制(带Random指针)
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head == nullptr){
return nullptr;
}
for(Node* node = head;node;node=node->next->next){
Node* newNode = new Node(node ->val);
newNode->next = node->next;
node->next = newNode;
}
for(Node* node = head;node;node=node->next->next){
Node* newNode = node->next;
newNode->random = (node->random==nullptr)?nullptr:node->random->next;
}
Node* newHead=head->next;
for(Node* node =head;node;node=node->next){
Node* newNode = node->next;
node->next = node->next->next;
newNode->next = (newNode->next==nullptr)?nullptr:newNode->next->next ;
}
return newHead;
}
};
链表加法计算
#include <endian.h>
class Solution {
public:
ListNode* reverseList(ListNode* head){
ListNode* p = nullptr;
ListNode* cur = head;
while(cur){
ListNode* n = cur->next;
cur->next=p;
p = cur;
cur = n;
}
return p;
}
int carry =0;
ListNode* addInList(ListNode* head1, ListNode* head2) {
if(head1 == nullptr){
return head2;
}
if(head2 == nullptr){
return head1;
}
head1 = reverseList(head1);
head2 = reverseList(head2);
ListNode* head = new ListNode(-1);
ListNode* nhead = head;
int carry = 0;
while(head1 != nullptr || head2 != nullptr){
int val =carry;
if(head1!=nullptr){
val += head1->val;
head1 = head1->next;
}
if(head2 != nullptr){
val += head2->val;
head2 = head2->next;
}
carry = val/10;
nhead->next = new ListNode(val%10);
nhead = nhead->next;
}
if(carry > 0){
nhead->next = new ListNode(carry);
}
return reverseList(head->next);
}
};
单链表的排序
class Solution {
public:
ListNode* sortInList(ListNode* head) {
if (head == nullptr ||head->next == nullptr){
return head;
}
ListNode* fast = head->next;
ListNode* slow = head;
while(fast != nullptr && fast->next != nullptr){
slow = slow->next;
fast = fast->next->next;
}
ListNode * temp = slow->next;
slow->next = nullptr;
ListNode* left = sortInList(head);
ListNode* right = sortInList(temp);
ListNode* h =new ListNode(0);
ListNode* res = h;
while(left != nullptr && right !=nullptr){
if(left->val < right->val){
h->next =left;
left = left->next;
}else{
h->next =right;
right = right->next;
}
h = h->next;
}
h->next = left!=nullptr ? left:right;
return res->next;
}
};
判断是否回文链表
#方法一(数组存储比较)
class Solution {
public:
bool isPail(ListNode* head) {
vector<int> nums;
while(head != NULL){
nums.push_back(head->val);
head = head->next;
}
vector<int> temp = nums;
reverse(temp.begin(),temp.end());
for(int i =0 ;i<nums.size();i++){
if(nums[i] != temp[i]){
return false;
}
}
return true;
}
};
#方法二(双指针中点)
class Solution {
public:
ListNode* reverse(ListNode* head){
ListNode* prev = NULL;
while(head != NULL){
ListNode* next = head->next;
head->next = prev;
prev = head;
head = next;
}
return prev;
}
bool isPail(ListNode* head) {
ListNode* p = head;
int n =0;
while(p != NULL){
n++;
p = p->next;
}
n = n/2;
p = head;
while(n>0){
p = p->next;
n--;
}
p = reverse(p);
ListNode* q =head;
while(p != NULL){
if(p->val !=q ->val){
return false;
}
p = p->next;
q = q->next;
}
return true;
}
};
找两个链表的第一个公共节点
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
ListNode* l1 = pHead1;
ListNode* l2 = pHead2;
while(l1!=l2){
l1 = (l1==nullptr?pHead2:l1->next);
l2 = (l2==nullptr?pHead1:l2->next);
}
return l1;
}
};
删除有序列表的重复元素(保留)
#include <climits>
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == nullptr){
return nullptr;
}
ListNode* cur = head;
while(cur != nullptr && cur->next != nullptr){
if(cur ->val == cur->next->val){
cur->next = cur->next->next;
}else{
cur = cur->next;
}
}
return head;
}
};
删除有序列表的重复元素(不保留)
#include <climits>
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == nullptr){return {};}
ListNode* hair = new ListNode(-1) ;
hair->next = head;
ListNode* cur = hair;
while(cur->next != nullptr && cur->next->next!=nullptr){
if(cur->next->val == cur->next->next->val){
int temp = cur->next->val;
while(cur->next != nullptr && cur->next->val == temp){
cur->next = cur->next->next;
}
}else{
cur = cur->next;
}
}
return hair->next;
}
};
链表的奇偶重排
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head==NULL ){
return {};
}
ListNode* even = head->next;
ListNode* odd = head;
ListNode* evenhead = even;
while(even != NULL && even->next!= NULL){
odd->next = odd->next->next;
odd = odd->next;
even -> next = even ->next ->next;
even = even->next;
}
odd->next = evenhead;
return head;
}
};