题目:
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = []
输出:[]
示例 3:
输入:l1 = [], l2 = [0]
输出:[0]
题解:
方法一:看看人家的暴力,我的太太太暴力了...第一个就应该想到这种方法。
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
if (list2 == null) {
return list1;
}
ListNode pHead = new ListNode(0);
ListNode current = pHead;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
current.next = list1;
list1 = list1.next;
} else if (list1.val > list2.val) {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}
if (list1 != null) {
current.next = list1;
}
if (list2 != null) {
current.next = list2;
}
return pHead.next;
}
}
public class Main{
public static void main (String []args){
ListNode l1 = new ListNode(1);//663
ListNode l2 = new ListNode(2);//664
ListNode l3 = new ListNode(4);//665
ListNode q1 = new ListNode(1);//666
ListNode q2 = new ListNode(3);//667
ListNode q3 = new ListNode(4);//668
l1.next = l2;
l2.next = l3;
q1.next = q2;
q2.next = q3;
Solution p = new Solution();
ListNode b = p.mergeTwoLists(l1, q1);
while (b != null){
System.out.println("结果:"+b.val);
b = b.next;
}
}
}
方法二:递归啊,哎。因为都是排好序的,所以如果l1.val < l2.val的话,那么就让l1.next当头节点,继续和l2进行比较,最后范围的是l1。终止条件是某一个链表为空了,就是另一个剩余的链表的值都更大,直接返回另一个链表就好了。
package test;
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) {
return l2;
}
if(l2 == null) {
return l1;
}
if(l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
public class Main{
public static void main (String []args){
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(4);
ListNode q1 = new ListNode(1);
ListNode q2 = new ListNode(3);
ListNode q3 = new ListNode(4);
l1.next = l2;
l2.next = l3;
q1.next = q2;
q2.next = q3;
Solution p = new Solution();
ListNode b = p.mergeTwoLists(l1, q1);
while (b != null){
System.out.println("结果:"+b.val);
b = b.next;
}
}
}
方法三:我自己写的,将链表转换为数组,合并后排序,然后再转换回链表。
package test;
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}else if(l2 == null){
return l1;
}
//定义l1,l2链表长度
int len1 = 0;
int len2 = 0;
//定义两个空链表,指向头节点
ListNode phead1 = l1;
ListNode phead2 = l2;
//找到两个链表长度
while(l1 != null){
len1++;
l1 = l1.next;
}
//不要忘了把l1和l2重新指向头节点
l1 = phead1;
while(l2 != null){
len2++;
l2 = l2.next;
}
l2 = phead2;
//定义两个数组,用来存放链表里的数据
int[] a = new int[len1];
int[] b = new int[len2];
//开始存数据
for(int i = 0; i < len1; i++){
a[i] = l1.val;
l1 = l1.next;
}
for(int i = 0; i < len2; i++){
b[i] = l2.val;
l2 = l2.next;
}
//将两个数组合并
int[] c = new int[a.length + b.length];
for(int i = 0; i < c.length; i++){
if(i < a.length){
c[i] = a[i];
}else{
c[i] = b[i - a.length];
}
}
//冒泡排序
int min;
for(int i = 0; i < c.length - 1; i++){
for(int j = 0; j < c.length - 1 -i; j++){
if(c[j] > c[j + 1]){
min = c[j + 1];
c[j + 1] = c[j];
c[j] = min;
}
}
}
//将数组里的数放入链表
ListNode head = new ListNode(c[0]);
ListNode list = head;
for(int i = 1; i < c.length; i++){
ListNode temp = new ListNode(c[i]);
list.next = temp;
list = list.next;
}
list = head;
return list;
}
}
public class Main{
public static void main (String []args){
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(4);
ListNode q1 = new ListNode(1);
ListNode q2 = new ListNode(3);
ListNode q3 = new ListNode(4);
l1.next = l2;
l2.next = l3;
q1.next = q2;
q2.next = q3;
Solution p = new Solution();
ListNode b = p.mergeTwoLists(l1, q1);
while (b != null){
System.out.println("结果:"+b.val);
b = b.next;
}
}
}