《剑指offer》
1. 参考文献
2. 链表
- 从尾到头打印链表
- 在 O(1) 时间内删除链表节点
- 删除链表中重复的结点
- 链表中倒数第 K 个结点
- 链表中环的入口结点
- 反转链表
- 合并两个排序的链表
- 复杂链表的复制
- 两个链表的第一个公共结点
#include<iostream>
#include<stack>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
struct RandomListNode {
int val;
RandomListNode* next;
RandomListNode* random;
RandomListNode(int x) : val(x), next(nullptr), random(nullptr) {}
};
void printListReveringly(ListNode* head)
{
stack<ListNode*> s;
ListNode* node = head;
while (node) {
s.push(node);
node = node->next;
}
while (!s.empty()) {
node = s.top();
s.pop();
cout << node->val << endl;
}
}
ListNode* deleteNode(ListNode* head, ListNode *tobeDelete)
{
if (head == NULL || tobeDelete == NULL)
return NULL;
if (tobeDelete->next != NULL)
{
ListNode* next = tobeDelete->next;
tobeDelete->val = next->val;
tobeDelete->next = next->next;
}
else
{
if (head == tobeDelete)
head = NULL;
else {
ListNode *cur = head;
while (cur->next != tobeDelete)
cur = cur->next;
cur->next = NULL;
}
}
return head;
}
ListNode* deleteDuplication(ListNode* head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode* next = head->next;
if (head->val == next->val) {
while(next !=NULL && head->val==next->val)
next = next->next;
return deleteDuplication(next);
}
else
{
head->next = deleteDuplication(head->next);
return head;
}
}
ListNode* FindKthToTail(ListNode* head, int k)
{
if (head == NULL)
return NULL;
ListNode *P1 = head;
while (P1 != NULL && k-- > 0)
P1 = P1->next;
if (k > 0)
return NULL;
ListNode* P2 = head;
while (P1 = P1->next){
P1 = P1->next;
P2 = P2->next;
}
return P2;
}
bool hashCycle(ListNode* head) {
if (head == NULL || head->next == NULL)
return NULL;
ListNode* slow = head, *fast = head;
while (fast != NULL && fast->next!= NULL) {
slow = slow->next;
fast = fast = fast->next->next;
if (slow == fast)
return true;
}
return false;
}
ListNode* EntryNodeOfLoop(ListNode* head)
{
if (head == NULL || head->next == NULL)
return NULL;
ListNode* slow = head, *fast = head;
do {
fast = fast->next->next;
slow = slow->next;
} while (slow != fast);
fast = head;
while (slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
ListNode* FindFirstCommonNode(ListNode* head1, ListNode* head2)
{
ListNode* l1 = head1, * l2 = head2;
while (l1 != l2){
l1 = (l1 == NULL) ? head2 : l1->next;
l1 = (l2 == NULL) ? head1 : l2->next;
}
return l1;
}
ListNode* ReverseList(ListNode* head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *next = head->next;
head->next = NULL;
ListNode* newHead = ReverseList(next);
next->next = head;
return newHead;
}
ListNode* Merge(ListNode* list1, ListNode* list2) {
ListNode* head = new ListNode(-1);
ListNode *cur = head;
while (list1 != NULL && list2 != NULL) {
if (list1->val <= list2->val) {
cur->next = list1;
list1 = list1->next;
}
else {
cur->next = list2;
list2 = list2->next;
}
cur = cur->next;
}
if (list1 != NULL)
cur->next = list1;
if (list2 != NULL)
cur->next = list2;
return head->next;
}
void deleteList(ListNode* head)
{
ListNode* node = head;
while (node) {
ListNode* temp=node;
node=node->next;
delete temp;
}
}
RandomListNode* clone(RandomListNode* head) {
if (head == nullptr)
return nullptr;
RandomListNode* cur = head;
while (cur != nullptr) {
RandomListNode* clone = new RandomListNode(cur->val);
clone->next = cur->next;
cur->next = clone;
cur = clone->next;
}
cur = head;
while (cur != nullptr) {
RandomListNode* clone = cur->next;
if (cur->random != nullptr)
clone->random = cur->random->next;
cur = clone->next;
}
cur = head;
RandomListNode* PCloneHead = head->next;
while (cur->next != nullptr) {
RandomListNode* next = cur->next;
cur->next = next->next;
cur = next;
}
return PCloneHead;
}
void test_printListReveringly()
{
cout << "打印所有链表的值" << endl;
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(4);
head->next->next->next->next = new ListNode(5);
printListReveringly(head);
deleteList(head);
}
void test_deleteDuplication()
{
cout << "打印所有链表的值" << endl;
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(4);
head->next->next->next->next = new ListNode(5);
printListReveringly(head);
deleteDuplication(head);
printListReveringly(head);
deleteList(head);
}
void test_FindKthToTail()
{
cout << "打印所有链表的值" << endl;
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(4);
head->next->next->next->next = new ListNode(5);
printListReveringly(head);
cout << "打印第K链表的值" << endl;
ListNode* headK = FindKthToTail(head, 2);
printListReveringly(headK);
deleteList(head);
}
int main()
{
test_FindKthToTail();
getchar();
return 0;
}