链表复制
This commit is contained in:
commit
59586d0eb6
@ -7,9 +7,6 @@
|
|||||||
namespace yaha {
|
namespace yaha {
|
||||||
class CQueue {
|
class CQueue {
|
||||||
public:
|
public:
|
||||||
CQueue() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void append_tail(int value);
|
void append_tail(int value);
|
||||||
int delete_head();
|
int delete_head();
|
||||||
|
|
||||||
@ -20,7 +17,22 @@ private:
|
|||||||
|
|
||||||
class String {
|
class String {
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* 剑指 Offer 09. 用两个栈实现队列
|
||||||
|
*
|
||||||
|
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d3i87/
|
||||||
|
* @param s
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
std::string replace_space(std::string s);
|
std::string replace_space(std::string s);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 剑指 Offer 20. 表示数值的字符串
|
||||||
|
*
|
||||||
|
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d6vi6/
|
||||||
|
* @param s
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
bool is_number(std::string s);
|
bool is_number(std::string s);
|
||||||
|
|
||||||
std::string reverse_left_words(std::string s, int n);
|
std::string reverse_left_words(std::string s, int n);
|
||||||
@ -31,7 +43,16 @@ public:
|
|||||||
int val;
|
int val;
|
||||||
ListNode *next;
|
ListNode *next;
|
||||||
ListNode(int x) : val(x), next(NULL) {}
|
ListNode(int x) : val(x), next(NULL) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 剑指 Offer 06. 从尾到头打印链表
|
||||||
|
*
|
||||||
|
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5dt66m/
|
||||||
|
* @param head
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
std::vector<int> reverse_print(ListNode *head);
|
std::vector<int> reverse_print(ListNode *head);
|
||||||
|
|
||||||
void reverse_stack(ListNode *head, std::vector<int> &res);
|
void reverse_stack(ListNode *head, std::vector<int> &res);
|
||||||
void reverse(ListNode *head, std::vector<int> &res);
|
void reverse(ListNode *head, std::vector<int> &res);
|
||||||
};
|
};
|
||||||
@ -74,6 +95,19 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::stack<int> _stk;
|
std::stack<int> _stk;
|
||||||
std::stack<int> _min_stk;
|
std::stack<int> _min_stk;
|
||||||
|
|
||||||
|
void reverse_stack(ListNode *head, std::vector<int> &res);
|
||||||
|
void reverse(ListNode *head, std::vector<int> &res);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 剑指 Offer 24. 反转链表
|
||||||
|
*
|
||||||
|
* @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/9pdjbm/
|
||||||
|
* @param head
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
ListNode *reverse_list(ListNode *head);
|
||||||
|
ListNode* reverse_list(ListNode* cur, ListNode* pre);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -29,4 +29,32 @@ std::vector<int> ListNode::reverse_print(ListNode *head) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ListNode* ListNode::reverse_list(ListNode* cur, ListNode* pre) {
|
||||||
|
if (cur == nullptr) {
|
||||||
|
return pre;
|
||||||
|
}
|
||||||
|
ListNode* res = reverse_list(cur->next, cur);
|
||||||
|
cur->next = pre;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
ListNode *ListNode::reverse_list(ListNode *head) {
|
||||||
|
ListNode* cur = head;
|
||||||
|
ListNode* pre = nullptr;
|
||||||
|
|
||||||
|
while (cur) {
|
||||||
|
// 链表转存
|
||||||
|
ListNode* tmp = cur->next;
|
||||||
|
// 链表断开
|
||||||
|
cur->next = pre;
|
||||||
|
// 将当前链表值赋值给最后一个
|
||||||
|
pre = cur;
|
||||||
|
cur = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pre;
|
||||||
|
|
||||||
|
// return reverse_list(head, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
#include "jianzhi_offer.h"
|
#include "jianzhi_offer.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 剑指 Offer 06. 从尾到头打印链表
|
||||||
|
*/
|
||||||
TEST(NodeListTest, BasicAssertions) {
|
TEST(NodeListTest, BasicAssertions) {
|
||||||
yaha::ListNode *node = new yaha::ListNode(1);
|
yaha::ListNode *node = new yaha::ListNode(1);
|
||||||
node->next = new yaha::ListNode(3);
|
node->next = new yaha::ListNode(3);
|
||||||
@ -22,3 +25,22 @@ TEST(NodeListStackTest, BasicAssertions) {
|
|||||||
ASSERT_EQ(3, res[1]);
|
ASSERT_EQ(3, res[1]);
|
||||||
ASSERT_EQ(1, res[2]);
|
ASSERT_EQ(1, res[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 剑指 Offer 24. 反转链表
|
||||||
|
*/
|
||||||
|
TEST(ListReverseTest, BasicAssertions) {
|
||||||
|
yaha::ListNode *node = new yaha::ListNode(1);
|
||||||
|
node->next = new yaha::ListNode(2);
|
||||||
|
node->next->next = new yaha::ListNode(3);
|
||||||
|
node->next->next->next = new yaha::ListNode(4);
|
||||||
|
node->next->next->next->next = new yaha::ListNode(5);
|
||||||
|
|
||||||
|
auto l = node->reverse_list(node);
|
||||||
|
ASSERT_EQ(5, l->val);
|
||||||
|
ASSERT_EQ(4, l->next->val);
|
||||||
|
ASSERT_EQ(3, l->next->next->val);
|
||||||
|
ASSERT_EQ(2, l->next->next->next->val);
|
||||||
|
ASSERT_EQ(1, l->next->next->next->next->val);
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user