Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc/lcof2 problems #639

Merged
merged 3 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lcof2/剑指 Offer II 029. 排序的循环链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,73 @@ class Solution {
}
```

### **C++**

```cpp
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;

Node() {}

Node(int _val) {
val = _val;
next = NULL;
}

Node(int _val, Node* _next) {
val = _val;
next = _next;
}
};
*/

class Solution {
public:
Node* insert(Node* head, int insertVal) {
Node* insert = new Node(insertVal);
if (head == nullptr) {
head = insert;
head->next = head;
} else if (head->next == nullptr) {
head->next = insert;
insert->next = head;
} else {
insertCore(head, insert);
}

return head;
}

void insertCore(Node* head, Node* insert) {
Node* cur = head;
Node* maxNode = head;
Node* next = head->next;

while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
cur = cur->next;
next = next->next;

if (cur->val >= maxNode->val)
maxNode = cur;
}

if (cur->val <= insert->val && insert->val <= next->val) {
insert->next = next;
cur->next = insert;
} else {
insert->next = maxNode->next;
maxNode->next = insert;

}

}
};
```

### **...**

```
Expand Down
62 changes: 62 additions & 0 deletions lcof2/剑指 Offer II 029. 排序的循环链表/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;

Node() {}

Node(int _val) {
val = _val;
next = NULL;
}

Node(int _val, Node* _next) {
val = _val;
next = _next;
}
};
*/

class Solution {
public:
Node* insert(Node* head, int insertVal) {
Node* insert = new Node(insertVal);
if (head == nullptr) {
head = insert;
head->next = head;
} else if (head->next == nullptr) {
head->next = insert;
insert->next = head;
} else {
insertCore(head, insert);
}

return head;
}

void insertCore(Node* head, Node* insert) {
Node* cur = head;
Node* maxNode = head;
Node* next = head->next;

while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
cur = cur->next;
next = next->next;

if (cur->val >= maxNode->val)
maxNode = cur;
}

if (cur->val <= insert->val && insert->val <= next->val) {
insert->next = next;
cur->next = insert;
} else {
insert->next = maxNode->next;
maxNode->next = insert;

}

}
};
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,73 @@ class Solution {
}
```

### **C++**

```cpp
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;

Node() {}

Node(int _val) {
val = _val;
next = NULL;
}

Node(int _val, Node* _next) {
val = _val;
next = _next;
}
};
*/

class Solution {
public:
Node* insert(Node* head, int insertVal) {
Node* insert = new Node(insertVal);
if (head == nullptr) {
head = insert;
head->next = head;
} else if (head->next == nullptr) {
head->next = insert;
insert->next = head;
} else {
insertCore(head, insert);
}

return head;
}

void insertCore(Node* head, Node* insert) {
Node* cur = head;
Node* maxNode = head;
Node* next = head->next;

while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
cur = cur->next;
next = next->next;

if (cur->val >= maxNode->val)
maxNode = cur;
}

if (cur->val <= insert->val && insert->val <= next->val) {
insert->next = next;
cur->next = insert;
} else {
insert->next = maxNode->next;
maxNode->next = insert;

}

}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,73 @@ class Solution {
}
```

### **C++**

```cpp
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;

Node() {}

Node(int _val) {
val = _val;
next = NULL;
}

Node(int _val, Node* _next) {
val = _val;
next = _next;
}
};
*/

class Solution {
public:
Node* insert(Node* head, int insertVal) {
Node* insert = new Node(insertVal);
if (head == nullptr) {
head = insert;
head->next = head;
} else if (head->next == nullptr) {
head->next = insert;
insert->next = head;
} else {
insertCore(head, insert);
}

return head;
}

void insertCore(Node* head, Node* insert) {
Node* cur = head;
Node* maxNode = head;
Node* next = head->next;

while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
cur = cur->next;
next = next->next;

if (cur->val >= maxNode->val)
maxNode = cur;
}

if (cur->val <= insert->val && insert->val <= next->val) {
insert->next = next;
cur->next = insert;
} else {
insert->next = maxNode->next;
maxNode->next = insert;

}

}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;

Node() {}

Node(int _val) {
val = _val;
next = NULL;
}

Node(int _val, Node* _next) {
val = _val;
next = _next;
}
};
*/

class Solution {
public:
Node* insert(Node* head, int insertVal) {
Node* insert = new Node(insertVal);
if (head == nullptr) {
head = insert;
head->next = head;
} else if (head->next == nullptr) {
head->next = insert;
insert->next = head;
} else {
insertCore(head, insert);
}

return head;
}

void insertCore(Node* head, Node* insert) {
Node* cur = head;
Node* maxNode = head;
Node* next = head->next;

while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
cur = cur->next;
next = next->next;

if (cur->val >= maxNode->val)
maxNode = cur;
}

if (cur->val <= insert->val && insert->val <= next->val) {
insert->next = next;
cur->next = insert;
} else {
insert->next = maxNode->next;
maxNode->next = insert;

}

}
};