两条链表合成一条升序链

public class test {
public static void main(String[] args) {
	Link l1 = new Link();
	Link l2 = new Link();
	Node n1 = new Node(1,null);
	Node n2 = new Node(2,null);
	Node n3 = new Node(3,null);
	Node n4 = new Node(4,null);
	l1.add(n1);
	l1.add(n4); 
	l1.print(); 
	System.out.println(l1.count());
	System.out.println("====");
	Link.len = 0; 
	l2.add(n2);
	l2.add(n3);
	l2.add(n1);
	l2.print();
	System.out.println(l2.count());
	System.out.println("====");
	connect c = new connect(); 
	Link l3 = c.mix(l1, l2);
	l3.print();
	 
}
}

public class Link {
	public Node header = null;
	static int len = 0;
	//增加节点
	public void add(Node n) {
		if(header == null) {
			header = n;
		}else {
			Node currentLast = findLast(header);
			currentLast.next = n;
		}
	}
	private Node findLast(Node n) {
		if(n.next == null) {
			return n;
		}
		return findLast(n.next);
	}
	public int count() {
		Node n = header;
		while(n != null) {
			len = len + 1;
			n = n.next;
		}
		return len;
	}
	//删去第k个节点
	public void remove(int k)  {
		 if(header == null) { System.out.println("error"); }else {
		    Node currentSecondLast = research(k - 1);
			//System.out.println("currentSecondLast.element"+currentSecondLast.element);
			Node removeNode = research(k);
			System.out.println("removeNode.element"+removeNode.element);
			if(removeNode.next!=null) {
			currentSecondLast.next =research(k+1);
			//System.out.println("currentSecondLast.next.element" + currentSecondLast.next.element);
			removeNode.next = null;
			}else {
				currentSecondLast.next = null;
			}
		}
	}
	//查找第i个节点(从1开始)
	private Node research(int i) {
		Node n = header;
		if(i == 1) {
			return n;
		}else{
			for(int j = 2;j<i + 1;j++) {
				n = n.next;
			}
			return n;
		}
		}
		

	public void print() {
		Node temp = header;
		while(temp != null) {
			System.out.print(temp.element + " ");
			temp = temp.next;
		}
		System.out.println();
	}
}

public class Node {
	int element;
	Node next;
	public Node() {}
	public Node(int element,Node next) {
		this.element = element;
		this.next = next;
	}
}

public class connect {
	//两条链表合成一条升序链
public Link mix(Link l1,Link l2) {
	System.out.println("开始了");
	Link l3 = new Link();
	Node com1 = l1.header;
	Node com2 = l2.header;
	Link.len = 0;
	int l1len = l1.count();
	Link.len = 0;
	int l2len = l2.count();
	Link.len = 0;
	int lens = l1len + l2len;
		int[] m = new int[lens];
		for(int i = 0;i<l1len;i++) {
			if(com1.next == null) {break;}
			m[i] = com1.element;
			com1 = com1.next;
		}
		for(int j = l1len;j<lens;j++) {
			m[j] = com2.element;
			if(com2.next == null) {break;}
			com2 = com2.next;
		}
		for(int i = 0;i<lens - 1;i++) {
			for(int j = 0;j<lens - 1 - i;j++) {
				if(m[j]>m[j+1]) {
					int tempt = m[j];
					m[j] = m[j+1];
					m[j+1] = tempt;
				}
			}
		}
		System.out.println();
		//System.out.println("合并后的链表(升序)是:");
		Node[] n1 = new Node[lens];
		for(int i = 0;i<lens;i++) {
			n1[i] = new Node(m[i],null);
			l3.add(n1[i]);
		}
		return l3;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值