2.25 leetcode 合并两个有序链表

my

import java.util.*;

class  Tttt
{
	public static void main(String[] args) 
	{
		
		 int[] a1 = {1,2,4};
		 int[] a2 = {1,3,4};
		 get(a1, a2);
	}
	public static LinkedList get(int[] a1,int[] a2)
	{
		List<Integer> link1 = Ints.asList(a1);
		//LinkedList<Integer> link1 = new LinkedList<Integer>(Arrays.asList(a1));
		//LinkedList<Integer> link1 = Arrays.asList(a1);
		//ArrayList< Integer> arrayList = new ArrayList< Integer>(a1.length);
		//Collections.addAll(arrayList, a1);
		LinkedList<Integer> link2 = new LinkedList<Integer>(Arrays.asList(a2));

		 /*
		 LinkedList<Integer> link1 = new LinkedList<Integer>();
		 link1.add(1);
		 link1.add(2);
		 link1.add(4);
		 sop(link1);
		 //List<Integer> link2 = new LinkedList<Integer>();
		 LinkedList<Integer> link2 = new LinkedList<Integer>();

		 link2.add(1);
		 link2.add(3);
		 link2.add(4);
		 sop(link2);
		 */
		link1.addAll(0,link2);
		sop(link1);
		Collections.sort(link1);
		sop(link1);
	}
		
		// List<Integer> linkf = new LinkedList<Integer>();
		//Iterator<Integer> it = link1.iterator();
/*
		while(it.hasNext())
		{
			Integer temp = it.next();
		
			sop(it.next)

		}
	}
	*/
	
	public static void sop(Object obj)
	{
		 System.out.println(obj);
	}

}


class MergeTwoSortedLists {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) 
		{
			//int val = 0;
			ListNode head = new ListNode(0);
			ListNode head = dummy;
			while(l1 != null && l2 !=null)
			{
				if (l1.val < l2.val)
				{
					head.next = new ListNode(l1.val);
					l1 = l1.next;
				}
				else
				{
					head.next = new ListNode(l2.val);
					l2 = l2.next;
				}
				head = head.next;
			}
			if(l1 != null) head.next = l1;
			if(l2 != null) head.next = l2;
			return dummy.next;
		}
	public static void main(String[] args)
	{
		MergeTwoSortedLists sol = new MergeTwoSortedLists();
		ListNode l1 = new ListNode(1);
		l1.next = new ListNode(2);
		l1.next.next = new ListNode(4);
		ListNode l2 = new ListNode(1);
		l2.next = new ListNode(3);
		l2.next.next = new ListNode(4);
		System.out.println(sol.mergeTwoLists(l1, l2));
	}

}

class ListNode {
		 int val;
		 ListNode next;
	 //ListNode构造函数,三个
		  ListNode() {}
		  ListNode(int val) { this.val = val; }
		  ListNode(int val, ListNode next) { this.val = val; this.next = next; }

		  public String toString()
		{
			ListNode head = this;
			StringBuilder sb = new StringBuilder();
			while (head != null)
			{
				sb.append(head.val + "");
				head = head.next;
			}
			return sb.toString();
		}

	  }
	  

	 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值