21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
//简单的链表合并
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *L = new ListNode(0), *p;
p = L;
while(l1&&l2)
{
if(l1->val < l2->val)
{
p->next = l1;
p = l1;
l1 = l1->next;
}
else
{
p->next = l2;
p = l2;
l2 = l2->next;
}
}
while(l1)
{
p->next = l1;
p = l1;
l1 = l1->next;
}
while(l2)
{
p->next = l2;
p = l2;
l2 = l2->next;
}
return L->next;
}
};
22. Generate
Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
//全排列公式扫一下就可以了,不会用STL的,可以手写dfs实现全排列
class Solution {
public:
bool check(string &s)
{
stack<char> st;
int len = s.length();
for(int i = 0; i < len; i++)
{
if(s[i] == '(') st.push(s[i]);
else
{
if(st.empty()) return false;
if(st.top() == '(') st.pop();
else return false;
}
}
return true;
}
vector<string> generateParenthesis(int n) {
vector<string> ans;
string s = "";
for(int i = 1; i <= n; i++) s += "()";
sort(s.begin(), s.end());
do
{
if(check(s)) ans.push_back(s);
}
while(next_permutation(s.begin(), s.end()));
return ans;
}
};
23. Merge
k Sorted Lists
//多个有序链表的合并,这个题貌似是zy学长在吃饭的时候教会我的,刚好用到这个题,复杂度O(n*log(k)),用优先队列去维护,这样贼快
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
typedef pair<int, int> P;
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<P, vector<P>, greater<P> > q;
int n = lists.size();
for(int i = 0; i < n; i++)
{
if(lists[i])
q.push(make_pair(lists[i]->val, i));
}
ListNode *L = new ListNode(0), *p;
p = L;
while(!q.empty())
{
P cur = q.top(); q.pop();
int i = cur.second;
p->next = lists[i];
p = lists[i];
lists[i] = lists[i]->next;
if(lists[i]) q.push(make_pair(lists[i]->val, i));
}
return L->next;
}
};
24. Swap
Nodes in PairsGiven a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
//换一下啊
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *p, *q, *nx, *pre = new ListNode(0), *L;
pre->next = head;
L = pre;
while(pre->next && pre->next->next)
{
p = pre->next;
q = p->next;
nx = q->next;
pre->next = q;
q->next = p;
p->next = nx;
pre = p;
}
head = L->next;
free(L);
return head;
}
};
25. Reverse
Nodes in k-GroupGiven a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
//栈的合理运用
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
stack<ListNode*> s;
ListNode *L = new ListNode(0), *p, *q, *x;
p = L;
p->next = head;
q = head;
while(q)
{
x = q;
for(int i = 1; i <= k; i++)
{
s.push(q);
q = q->next;
if(!q) break;
}
if(s.size() == k)
{
while(!s.empty())
{
ListNode *cur = s.top();
s.pop();
p->next = cur;
p = cur;
}
p->next = NULL;
}
else
{
p->next = x;
break;
}
}
head = L->next;
free(L);
return head;
}
};