LinkedList
链表总结
1 | |
常见题型
给定链表,删除所有重复元素,使得每一个元素只出现一次
1 | |
1 | |
给定链表,删除所有的重复元素
- 树是递归定义的,因此可以用递归求解
1 | |
翻转链表
非递归
1
2
3
4
5
6
7
8
9
10
11
12
13class Solution {
public:
ListNode *reverseList(ListNode *head) {
ListNode *cur = nullptr;
while (head) {
ListNode *next = head->next;
head->next = cur;
cur = head;
head = next;
}
return cur;
}
};递归
1
2
3
4
5
6
7
8
9
10
11class Solution {
public:
ListNode *reverseList(ListNode *head){
if (!head || !head->next)
return head;
ListNode *node = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return node;
}
};
Merge Two Lists
1 | |