【JAVA、C++】LeetCode 021 Merge Two Sorted Lists
static public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode listnode=new ListNode(0);
ListNode index=listnode;
while(l1!=null&&l2!=null){
if(l1.val>l2.val){
ListNode temp=l1;
l1=l2;
l2=temp;
}
index.next=new ListNode(l1.val);
index=index.next;
l1=l1.next;
}
if(l1==null)
index.next=l2;
else
index.next=l1;
return listnode.next;
}
C++:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return l2;
else if (l2 == NULL)
return l1;
ListNode* start;
if (l1->val < l2->val) {
start = l1;
l1 = l1->next;
}
else {
start = l2;
l2 = l2->next;
}
ListNode* cur = start;
while (l1 != NULL&&l2 != NULL) {
if (l1->val < l2->val) {
cur->next = l1;
cur = cur->next;
l1 = l1->next;
}
else {
cur->next = l2;
cur = cur->next;
l2 = l2->next;
}
}
if (l1 == NULL)
cur->next = l2;
else
cur->next = l1;
return start;
}
};
【JAVA、C++】LeetCode 021 Merge Two Sorted Lists的更多相关文章
- Leetcode 021 Merge Two Sorted Lists
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- Java String是不可变对象
基本数据类型和String类型都是值传递,数组,对象等是引用传递 经多方面查找,String很奇特,虽然是引用数据类型,但是采用的却是值传递!!!基本数据类型采用的都是值传递,数组和对象都是引用传递( ...
- 【Gym 100971J】Robots at Warehouse
题意 链接给你一个n*m的地图,'#'代表墙,‘.’代表可走的,1代表1号机器人,2代表2号机器人,机器人可以上下左右移动到非墙的位置,但不能走到另一个机器人身上.问能否交换1和2的位置. 分析 如果 ...
- 【poj3070】 Fibonacci
http://poj.org/problem?id=3070 (题目链接) 题意 用矩阵乘法求fibonacci数列的第n项. Solution 矩乘入门题啊,题目把题解已经说的很清楚里= =. 矩乘 ...
- JSON前端页面解析
JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * JSON 具有自我描述性,更 ...
- windows上使用image库
首先要安装这个库,可以使用pip安装,那么我们要首先安装pip 去https://bootstrap.pypa.io/get-pip.py下载get-pip.py,然后运行python get-pip ...
- KxMenu下拉菜单
+ (void)createMenu:(id)sender target:(UIViewController *)t { NSArray *menuItems = @[ [KxMenuItem men ...
- java中获取路径中的空格处理(%20)问题
在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...
- VMWare提供了三种工作模式上网
VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...
- 通过cmd修改注册表并设置cmd窗口的大小
设置cmd的窗口 mode: modem设置系统设备,主要是lpt1, com1/2, con: 启动时设置窗口大小: cmd /k "mode con: cols=120 lines=40 ...
- apache 配置多个虚拟主机
修改文件:httd.conf 文件地址:D:\wamp\bin\apache\Apache2.2.21\conf #配置虚拟主机<VirtualHost 127.0.0.3:80>Serv ...