剑指offer-数据结构二——链表
剑指offer-数据结构
链表
1.从尾到头打印链表
面试题6:从尾到头打印链表
题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。
思路分析:借助栈来实现从尾到头打印链表
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> vec;
if (head == nullptr) return vec;
stack<int> sta;
ListNode* ptr = head;
while (ptr != nullptr) {
sta.push(ptr -> val);
ptr = ptr -> next;
}
while(!sta.empty()) {
vec.push_back(sta.top());
sta.pop();
}
return vec;
}
};
2. 输入一个链表,输出该链表中倒数第k个结点。
**思路分析:**从链表头开始遍历,首先计算出链表的长度,倒数第k个位置的节点,即是顺数length-k+1个位置的节点,在从头开始遍历至length-k+1个位置即可,这种思路需要遍历两遍链表为了只遍历一遍链表,可以使用两个指针。
(1)遍历两次链表的做法:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
ListNode* pnode = nullptr;
if (pListHead == nullptr) return pnode;
int length = 0;
pnode = pListHead;
while (pnode != nullptr) {
// 计算链表的长度
length++;
pnode = pnode -> next;
}
if (k > length)
return nullptr;
else {
int posk = length - k;
int index = 0;
pnode = pListHead;
while (index < posk) {
pnode = pnode -> next;
index++;
}
return pnode;
}
}
};
**(2)只遍历一次链表的做法:**需要设置两个指针。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
ListNode* phead = nullptr;
if (pListHead == nullptr) return phead;
phead = pListHead;
ListNode* ptail = pListHead;
for (int i = 0; i < k; i++) {
if (phead != nullptr) {
phead = phead -> next;
} else {
// 表明k比链表的长度还要大
return nullptr;
}
}
while (phead != nullptr) {
phead = phead -> next;
ptail = ptail -> next;
}
return ptail;
}
};
3.输入一个链表,反转链表后,输出新链表的表头。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* head = nullptr;
if (pHead == nullptr) return head;
head = pHead;
ListNode* temp1 = head -> next;
ListNode* temp2 = head -> next;
head -> next = nullptr;
while (temp1 != nullptr) {
temp2 = temp1 -> next;
temp1 -> next = head;
head = temp1;
temp1 = temp2;
}
return head;
}
};
4. 合并两个排序的链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
if (pHead1 == nullptr && pHead2 == nullptr)
return nullptr;
else if (pHead1 == nullptr)
return pHead2;
else if (pHead2 == nullptr)
return pHead1;
ListNode* head = nullptr;
ListNode* tail = nullptr;
while (pHead1 != nullptr && pHead2 != nullptr) {
if (pHead1 -> val <= pHead2 -> val) {
if (head == nullptr) {
head = tail = pHead1;
pHead1 = pHead1 -> next;
head -> next = tail -> next = nullptr;
} else {
tail -> next = pHead1;
pHead1 = pHead1 -> next;
tail = tail -> next;
tail -> next = nullptr;
}
} else {
if (head == nullptr) {
head = tail = pHead2;
pHead2 = pHead2 -> next;
head -> next = tail -> next = nullptr;
} else {
tail -> next = pHead2;
pHead2 = pHead2 -> next;
tail = tail -> next;
tail -> next = nullptr;
}
}
}
if (pHead1 != nullptr) {
tail -> next = pHead1;
} else if (pHead2 != nullptr) {
tail -> next = pHead2;
}
return head;
}
};
5. 两个链表的第一个公共节点
输入两个链表,找出它们的第一个公共结点。栈是呈Y字型的。
思路分析:
思路一: 首先计算出两个链表的长度,计算出两个链表的长度差count,将指向比较长的链表的指针向前移动count位,然后两个链表的指针一起移动,找到第一个指向相同节点的两个指针,返回这个指针即可。时间复杂度是O(m + n).
思路二: 使用两个栈来分别保存两个链表的元素,然后从栈顶开始比较两个栈的栈顶元素,如果元素相等,那么记录当前相等的元素,并且将栈顶元素从两个栈弹出,直到两个栈的栈顶元素不再相等为止,输出记录下的相等的元素即可,这种方式的时间复杂度是O(m + n),空间复杂度也是O(m + n).
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
if (pHead1 == nullptr || pHead2 == nullptr)
return nullptr;
int length1 = 0;
int length2 = 0;
ListNode* pnode1 = pHead1;
ListNode* pnode2 = pHead2;
while (pnode1 != nullptr) {
++length1;
pnode1 = pnode1 -> next;
}
while (pnode2 != nullptr) {
++length2;
pnode2 = pnode2 -> next;
}
pnode1 = pHead1;
pnode2 = pHead2;
if (length1 > length2) {
int count = length1 - length2;
while (count--) {
if (pnode1 != nullptr) {
pnode1 = pnode1 -> next;
} else {
return nullptr;
}
}
} else if (length2 > length1) {
int count = length2 - length1;
while (count--) {
if (pnode2 != nullptr) {
pnode2 = pnode2 -> next;
} else {
return nullptr;
}
}
}
while (pnode1 && pnode2) {
if (pnode1 == pnode2) {
return pnode1;
}
pnode1 = pnode1 -> next;
pnode2 = pnode2 -> next;
}
return nullptr;
}
};