关于链表
//
// Created by Administrator on 2021/8/1.
//
#ifndef C__TEST01_LINKNODE_HPP
#define C__TEST01_LINKNODE_HPP
#include <map>
#include <unordered_map>
#include <unordered_set>
class LinkNode{
public:
int value;
LinkNode *next;
LinkNode(int value):value(value){}
};
/*
* circle = true, LinkList has a circle
* circle = false, LinkList doesn't have a circle
* */
LinkNode* linkInitDouble(bool circle = false){
LinkNode *head = new LinkNode(3);
head->next = NULL;
if(circle){
LinkNode *L = new LinkNode(rand()%10);
head->next = L;
L->next = NULL;
LinkNode *p = L;
for (int i = 0; i < 6; ++i) {
L = new LinkNode(rand()%10);
L->next = NULL;
p->next = L;
p = L;
}
p = head;
for(int i = 0; i<3 + rand()%3; ++i){
p = p->next;
}
L->next = p;
}else{
LinkNode *L = new LinkNode(rand()%10);
head->next = L;
L->next = NULL;
LinkNode *p = L;
for (int i = 0; i < 6; ++i) {
L = new LinkNode(rand()%10);
L->next = NULL;
p->next = L;
p = L;
}
}
return head;
}
/*
* circle = true, LinkList has a circle
* circle = false, LinkList doesn't have a circle
* */
LinkNode* linkInitSingle(bool circle = false){
LinkNode *head = new LinkNode(3);
head->next = NULL;
if(circle){
LinkNode *L = new LinkNode(rand()%10);
head->next = L;
L->next = NULL;
LinkNode *p = L;
for (int i = 0; i < 7; ++i) {
L = new LinkNode(rand()%10);
L->next = NULL;
p->next = L;
p = L;
}
p = head;
for(int i = 0; i<3 + rand()%3; ++i){
p = p->next;
}
L->next = p;
}else{
LinkNode *L = new LinkNode(rand()%10);
head->next = L;
L->next = NULL;
LinkNode *p = L;
for (int i = 0; i < 7; ++i) {
L = new LinkNode(rand()%10);
L->next = NULL;
p->next = L;
p = L;
}
}
return head;
}
LinkNode *getMidLink(LinkNode *L){
LinkNode *slow = L;
LinkNode *fast = L->next;//get mid+1
//LinkNode *fast = L->next; get mid
while (fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
if(fast == NULL){
return slow;
}
}
return slow;
}
bool isCircle(LinkNode *L){
LinkNode *slow = L;
LinkNode *fast = L->next->next;
while(slow != fast){
slow = slow->next;
fast = fast->next->next;
if(fast == NULL || fast->next == NULL){
return false;
}
}
return true;
}
LinkNode *getEnterCirclePointer(LinkNode *L){
LinkNode *slow = L->next;
LinkNode *fast = L->next->next;
while(slow != fast){
if(fast == NULL || fast->next == NULL){
return NULL;
}
slow = slow->next;
fast = fast->next->next;
}
fast = L;
while(slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
LinkNode *getEnterCirclePointerByHash(LinkNode *L){
unordered_set<LinkNode*> hashSet;
while(L != NULL){
hashSet.insert(L);
L = L->next;
if(hashSet.find(L) != hashSet.end()){
return L;
}
}
return L;
}
/*题目:
* 对于一个链表,分成小于一个阈值,等于一个阈值,大于一个阈值三个部分
* 三个阈值都一样
* */
LinkNode* dutchFlagByLinkList(LinkNode *L, int value){
LinkNode *minStart = NULL;
LinkNode *minEnd = NULL;
LinkNode *equalStart = NULL;
LinkNode *equalEnd = NULL;
LinkNode *maxStart = NULL;
LinkNode *maxEnd = NULL;
LinkNode *start = NULL;
while(L != NULL){
if(L->value < value){
if(minStart == NULL){
minStart = L;
minEnd = L;
}else{
minEnd->next = L;
minEnd = L;
}
}else if(L->value == value){
if(equalStart == NULL){
equalStart = L;
equalEnd = L;
}else{
equalEnd->next = L;
equalEnd = L;
}
}else{
if(maxStart == NULL){
maxStart = L;
maxEnd = L;
}else{
maxEnd->next = L;
maxEnd = L;
}
}
L = L->next;
}
if(minStart != NULL){
minEnd->next = equalStart;
equalEnd = equalEnd != NULL? equalEnd:minEnd;
}
if(equalEnd != NULL){
equalEnd->next = maxStart;
}
return minStart != NULL? minStart:(equalStart!=NULL?equalStart:maxStart);
}
#endif //C__TEST01_LINKNODE_HPP
主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈