两数相加
考虑创建哑节点dummy,使用dummy->next表示真正的头节点,避免空边界. 时间复杂度
O
(
n
)
O(n)
O(n)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* first = new ListNode(0); // 哑结点
ListNode* last = first;
int carry = 0;
while(l1 or l2 or carry){
// 相加
int bitsum = 0;
if(l1){
bitsum += l1->val;
l1 = l1->next;
}
if(l2){
bitsum += l2->val;
l2 = l2->next;
}
if(carry){
bitsum += carry;
}
// 结果
int digit = bitsum % 10;
carry = bitsum / 10;
// 链表存储
ListNode* node = new ListNode(digit);
last->next = node;
last = node;
}
last = first->next;
delete first;
return last;
}
};
反转链表输出
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
方法一:使用std::reverse()
反转链表的输出vector
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
std::vector<int> ret;
while(head!=nullptr){
ret.push_back(head->val);
head= head->next;
}
//再开辟内存,逆序遍历
//for(auto iter=arr.rbegin(); iter<arr.rend();iter++){
// ret.push_back(*iter);
//}
std::reverse(ret.begin(), ret.end());
return ret;
}
};
方法二:直接反转链表,改变指针指向,见下。
反转链表
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
ListNode* newHead = nullptr; // 必须设置为空指针
ListNode* node; // 临时保存删除的结点,也是新插入的结点
while(head){
node = head; // 保存删除
head = head->next; // 更新头
node->next = newHead; // 重新插入
newHead = node; // 更新头
}
return newHead;
}
};
合并两个有序链表
可以改变原有链表结构,不需要额外申请空间。
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
ListNode* head = new ListNode(0); // 哑结点
ListNode* p = head; // 遍历指针
while(pHead1 && pHead2){
if(pHead1->val < pHead2->val){
p->next = pHead1;
pHead1 = pHead1->next;
}
else{
p->next = pHead2;
pHead2 = pHead2->next;
}
p = p->next;
}
p->next = pHead1 ? pHead1 : pHead2;
return head->next;
}
寻找循环链表的入口
题目描述:
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
// 判断是否存在环
ListNode* fast = pHead;
ListNode* slow = pHead;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
if(fast == slow) break;
}
if(!fast || !fast->next) return NULL;
// 寻找环入口
fast = pHead;
while(fast != slow){
fast = fast->next;
slow = slow->next;
}
return fast;
}
};
计算循环链表中环的长度
关键点:
- 判断存在循环,进入环中
- 判断
slow->next != fast
,长度加1
int getLoopLength(ListNode *pHead) {
ListNode *fast(pHead), *slow(pHead);
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
break;
}
}
if (!fast || !fast->next) return -1;
int length = 1; // 从1开始
while (slow->next != fast) {
slow = slow->next;
length++;
}
return length;
}
删除链表中的重复元素
关键点:
- 使用map记录出现的次数
- 删除链表结点需要两个遍历指针:前一结点和当前结点
class Solution {
public:
ListNode *deleteDuplication(ListNode *pHead) {
map<int, int> record; // val:count
ListNode *cur = pHead;
while (cur) {
if (record.count(cur->val)) {
record[cur->val]++;
} else {
record[cur->val] = 0;
}
cur = cur->next;
}
for (auto x : record) {
cout << x.first << ":" << x.second << endl;
}
ListNode *vHead = new ListNode(-1);
vHead->next = pHead;
ListNode *pre = vHead; // 前一结点
while (pHead) { // 当前结点
if (record[pHead->val]) { // 无效结点
pre->next = pHead->next; // 重新链接
pHead = pHead->next; // 下一个
}
else{ // 有效结点
pre = pre->next; // 下一个
pHead = pHead->next; // 下一个
}
}
return vHead->next;
}
};
输出链表倒数第K个结点
分析:
用双指针法,找倒数第K个,就让fast指针先前进K个结点
ListNode *findKthTail(ListNode *pHead, int K) {
ListNode *p1(pHead), *p2(pHead);
for(int i=0; i<K; ++i){
p1 = p1-> next;
if(!p1){
return nullptr;
}
}
while(p1){
p1 = p1-> next;
p2 = p2-> next;
}
return p2;
}