leetcode 题解代码整理 21

/** * 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 *ans,*head;if (l1==NULL && l2==NULL ) return NULL;if (l1==NULL){head=l2;l2=l2->next;}elseif (l2==NULL){head=l1;l1=l1->next;}else{if (l1->val < l2->val){head=l1;l1=l1->next;}else{head=l2;l2=l2->next;}}ans=head;while (l1!=NULL || l2!=NULL){if (l1==NULL){ans->next=l2;ans=l2;l2=l2->next;}elseif (l2==NULL){ans->next=l1;ans=l1;l1=l1->next;}else{if (l1->val < l2->val){ans->next=l1;ans=l1;l1=l1->next;}else{ans->next=l2;ans=l2;l2=l2->next;}}}return head;}};

Generate Parentheses

Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, givenn= 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

输出所有的括号匹配

class Solution {vector<string>ans;public:vector<string> generateParenthesis(int n){string mark="";dfs(mark,n,n);return ans;}private:void dfs(string mark,int x,int y){if (x+y==0){ans.push_back(mark);return ;}if (x!=0)dfs(mark+"(",x-1,y);if (x<y)dfs(mark+")",x,y-1);}};Merge k Sorted Lists

Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.

对K个有序链表排序

/** * Definition for singly-linked list. * struct ListNode { *int val; *ListNode *next; *ListNode(int x) : val(x), next(NULL) {} * }; */class ListNodeCompare{public:bool operator()(ListNode *l1,ListNode *l2){return l1->val > l2->val;}};class Solution {public:ListNode* mergeKLists(vector<ListNode*>& lists){ListNode *ans,*head,*p;priority_queue<ListNode*,vector<ListNode*>,ListNodeCompare>q;if (lists.size()==0) return NULL;ans=(ListNode*)malloc(sizeof(ListNode));ans->next=NULL;head=ans;for (int i=0;i<lists.size();i++)if (lists[i]!=NULL)q.push(lists[i]);while (!q.empty()){p=q.top();q.pop();ans->next=p;ans=ans->next;if (p->next!=NULL)q.push(p->next);}return head->next;}};Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example,Given1->2->3->4, you should return the list as2->1->4->3.

Your algorithm should use only constant space. You maynotmodify 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 *p1,*pre,*p2;if (head==NULL || head->next==NULL) return head;p1=head;pre=head;while (1){p2=p1->next;p1->next=p2->next;p2->next=p1;if (pre==head)head=p2;elsepre->next=p2;pre=p1;p1=p1->next;if (p1==NULL || p1->next==NULL) break;}return head;}};

Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.

If the number of nodes is not a multiple ofkthen 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

Fork= 2, you should return:2->1->4->3->5

Fork= 3, you should return:3->2->1->4->5

对链表中每k个节点进行一次反转

class Solution {public:ListNode* reverseKGroup(ListNode* head, int k){int n=k;int len=0;ListNode *p=head;if (head==NULL || head->next==NULL || k<=1)return head;while (p){len++;p=p->next;}if (n>len) return head;ListNode *q=head;while (n–){ListNode *ne=q->next;q->next=p;p=q;q=ne;}if (len-k>=k)head->next= reverseKGroup(q,k);elsehead->next=q;return p;}};

,旅行是一种病,当你把身边的人都传染了,

leetcode 题解代码整理 21

相关文章:

你感兴趣的文章:

标签云: