leetcode 题解代码整理 21-25题

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.

合并两个有序链表

/**
 * 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;
        }
        else 
        if (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;
            }
            else 
            if (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

 

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:

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


输出所有的括号匹配
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

 
Merge k sorted 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,
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 *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;
            else 
                pre->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 list k at a time and return its modified 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

对链表中每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);
        else
            head->next=q;
        return p;
    }
};



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值