博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Merge Two Sorted Lists Leetcode
阅读量:5094 次
发布时间:2019-06-13

本文共 3770 字,大约阅读时间需要 12 分钟。

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.

思想和merge sort一样,只是要考虑相等的情况的时候该怎么办。

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if (l1 == null) {            return l2;        }        if (l2 == null) {            return l1;        }        ListNode result = new ListNode(0);        ListNode returnResult = result;        while (l1 != null && l2 != null) {            while (l1 != null && l2 != null && l1.val < l2.val) {                result.next = l1;                l1 = l1.next;                result = result.next;            }            while (l1 != null && l2 != null && l1.val > l2.val) {                result.next = l2;                l2 = l2.next;                result = result.next;            }            while (l1 != null && l2 != null && l1.val == l2.val) {                result.next = l1;                l1 = l1.next;                result = result.next;                result.next = l2;                l2 = l2.next;                result = result.next;            }                    }        if (l1 == null) {            result.next = l2;        } else {            result.next = l1;        }        return returnResult.next;    }}

写的还是有些冗余了。。。

简洁版:

public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if (l1 == null) {            return l2;        }        if (l2 == null) {            return l1;        }        ListNode result = new ListNode(0);        ListNode returnResult = result;        while (l1 != null && l2 != null) {            if (l1.val < l2.val) {                result.next = l1;                l1 = l1.next;            } else {                result.next = l2;                l2 = l2.next;            }            result = result.next;        }        if (l1 == null) {            result.next = l2;        } else {            result.next = l1;        }        return returnResult.next;    }}

没想到还可以用recursion做

public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if (l1 == null) {            return l2;        }        if (l2 == null) {            return l1;        }        ListNode result = null;        if (l1.val < l2.val) {            result = l1;            result.next = mergeTwoLists(l1.next, l2);        } else {            result = l2;            result.next = mergeTwoLists(l1, l2.next);        }        return result;    }}

 

时隔两个月再来做这道题,发现自己还是进步了的~至少直接写出了简洁版。吼吼吼

也又写了一遍recursion

public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if (l1 == null) {            return l2;        }        if (l2 == null) {            return l1;        }        if (l1.val > l2.val) {            l2.next = mergeTwoLists(l1, l2.next);            return l2;        } else {            l1.next = mergeTwoLists(l1.next, l2);            return l1;        }    }}

 

C++版本:

/** * 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) {        if (l1 == NULL) {            return l2;        }        if (l2 == NULL) {            return l1;        }        if (l1 -> val > l2 -> val) {            l2 -> next = mergeTwoLists(l1, l2 -> next);            return l2;        } else {            l1 -> next = mergeTwoLists(l1 -> next, l2);            return l1;        }    }};

 c++另一种写法怎么也写不过。。。不懂指针什么的,到时候懂了再来写吧。。。#未完待续#需回顾。。。

转载于:https://www.cnblogs.com/aprilyang/p/6620460.html

你可能感兴趣的文章
HDU4405(期望DP)
查看>>
Linux下svn的部署
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
Linux下MySQL数据库的备份与还原
查看>>
vs code 的便捷使用
查看>>
RPM查询篇
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
OC语法基本使用
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
SVN服务的配置与管理
查看>>
vim插件ctags的安装和使用
查看>>
个人总结
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
[Data Structure & Algorithm] 有向无环图的拓扑排序及关键路径
查看>>
git 常用命令
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>