文章目录
Leetcode 面试题24. 反转链表
问题描述
定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。
解题报告
略。
实现代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode*temp=NULL,*cur=head,*next;
while(cur!=NULL){
next=cur->next;
cur->next=temp;
temp=cur;
cur=next;
}
return temp;
}
};
参考资料
Leetcode 2. 两数相加
问题描述
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
解题报告
直接计算。
实现代码
/**
* 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* cur_1 = l1;
ListNode* cur_2 = l2;
ListNode* new_head = nullptr;
ListNode* new_tail = nullptr;
int carry = 0; //低位是否有进位
while (cur_1 || cur_2)
{
//创建结点
int x = cur_1 ? cur_1->val : 0;
int y = cur_2 ? cur_2->val : 0;
int val = x + y + carry;
carry = val / 10;
ListNode* node = new ListNode(val % 10);
//插入新链表
if (new_head == nullptr)
{
new_head = node;
new_tail = node;
}
else //尾插
{
new_tail->next = node;
new_tail = new_tail->next;
}
if (cur_1)
{
cur_1 = cur_1->next;
}
if (cur_2)
{
cur_2 = cur_2->next;
}
}
if (carry == 1) //最高位进位
{
new_tail->next = new ListNode(1);
new_tail = new_tail->next;
}
return new_head;
}
};
参考资料
[1] Leetcode 2. 两数相加
[2] Leetcode 445 题解区:Eric
Leetcode 445. 两数相加 II
问题描述
给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
进阶:
如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。
解题报告
- 直接计算
- 使用栈实现反转链表
实现代码
class Solution {
public:
//方法1
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
//对两个链表进行反转
l1 = list_reverse(l1);
l2 = list_reverse(l2);
//使用 题目2. 两数相加 函数
ListNode* head = addTwoNumbers_inverse(l1, l2);
//再将l1、l2反转回去
l1 = list_reverse(l1);
l2 = list_reverse(l2);
//将结果反转
return list_reverse(head);
}
ListNode* list_reverse(ListNode* head)
{
ListNode* prev = nullptr;
ListNode* cur = head;
while(cur)
{
ListNode* next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
return prev;
}
//题目2. 两数相加 函数
ListNode* addTwoNumbers_inverse(ListNode* l1, ListNode* l2) {
ListNode* cur_1 = l1;
ListNode* cur_2 = l2;
ListNode* new_head = nullptr;
ListNode* new_tail = nullptr;
int carry = 0; //低位是否有进位
while (cur_1 || cur_2)
{
//创建结点
int x = cur_1 ? cur_1->val : 0;
int y = cur_2 ? cur_2->val : 0;
int val = x + y + carry;
carry = val / 10;
ListNode* node = new ListNode(val % 10);
//插入新链表
if (new_head == nullptr)
{
new_head = node;
new_tail = node;
}
else //尾插
{
new_tail->next = node;
new_tail = new_tail->next;
}
if (cur_1)
{
cur_1 = cur_1->next;
}
if (cur_2)
{
cur_2 = cur_2->next;
}
}
if (carry == 1) //最高位进位
{
new_tail->next = new ListNode(1);
new_tail = new_tail->next;
}
return new_head;
}
2、使用栈 并 逆序构建结果链表
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> stack1;
while (l1)
{
stack1.push(l1->val);
l1 = l1->next;
}
stack<int> stack2;
while (l2)
{
stack2.push(l2->val);
l2 = l2->next;
}
ListNode* prev = nullptr;
ListNode* node = nullptr;
int carry = 0;
while (!stack1.empty() || !stack2.empty())
{
int x = stack1.empty() ? 0 : stack1.top();
int y = stack2.empty() ? 0 : stack2.top();
if (!stack1.empty())
{
stack1.pop();
}
if (!stack2.empty())
{
stack2.pop();
}
int val = x + y + carry;
carry = val / 10;
prev = node;
node = new ListNode(val % 10);
node->next = prev;
}
if (carry == 1)
{
prev = node;
node = new ListNode(1);
node->next = prev;
}
return node;
}
};
参考资料
[1] Leetcode 445. 两数相加 II
[2] 题解区:Eric