链表合并之一

  1 package LinkTogether;
  2 
  3 /*
  4  * 两个升序链表合并,降序输出,使用栈
  5  * */
  6 
  7 import java.util.Stack;
  8 
  9 class ListNode {
 10     int val;
 11     ListNode next = null;
 12 
 13     ListNode(int val) {
 14         this.val = val;
 15     }
 16 }
 17 
 18 public class Demo03 {
 19 
 20     public static void main(String[] args) {
 21         ListNode n1 = new ListNode(10);            //第一个链表
 22         ListNode n2 = new ListNode(20);
 23         ListNode n3 = new ListNode(30);
 24         ListNode n4 = new ListNode(40);
 25         n1.next = n2;
 26         n2.next = n3;
 27         n3.next = n4;
 28         
 29         ListNode n5 = new ListNode(50);            //第二个链表
 30         ListNode n6 = new ListNode(60);
 31         ListNode n7 = new ListNode(70);
 32         ListNode n8 = new ListNode(80);
 33         n5.next = n6;
 34         n6.next = n7;
 35         n7.next = n8;
 36         
 37         n1 = Merge(n1,n5);                        //调用合并函数
 38         
 39         print(n1);                                //输出
 40     }
 41     public static ListNode Merge(ListNode list1,ListNode list2) {
 42         if(list1 == null && list2 == null)return null;        //如果两个链表都位空,则输出空
 43         if(list1 == null)return list2;                        //如果链表一为空,则合并后的链表即是链表二
 44         if(list2 == null)return list1;                        //如果链表二为空,则合并后的链表即是链表三
 45         Stack<Integer> stk = new Stack<Integer>();            //声明栈
 46         if(list1.val <= list2.val){                            //链表头部入栈
 47             stk.push(list1.val);
 48             stk.push(list2.val);
 49         }
 50         if(list1.val > list2.val){
 51             stk.push(list2.val);
 52             stk.push(list1.val);
 53         }
 54         ListNode temp1 = list1;
 55         ListNode temp2 = list2;
 56         while(temp1.next != null || temp2.next != null){
 57             if(temp1.next != null && temp2.next != null){
 58                 if(temp1.next.val <= temp2.next.val){
 59                     stk.push(temp1.next.val);
 60                     stk.push(temp2.next.val);
 61                 }
 62                 if(temp1.next.val >temp2.next.val){
 63                     stk.push(temp2.next.val);
 64                     stk.push(temp1.next.val);
 65                 }
 66             }
 67             if(temp1.next != null && temp2.next == null){
 68                 stk.push(temp1.next.val);
 69             }
 70             if(temp1.next == null && temp2.next != null){
 71                 stk.push(temp2.next.val);
 72             }
 73             temp1 = temp1.next;
 74             temp2 = temp2.next;
 75         }
 76         
 77         int flag = 0;                    //将链表二连在链表一的尾部
 78         ListNode temp = list1;
 79         while(flag<2){
 80             if(temp.next == null){
 81                 flag ++;
 82                 if(flag == 2)break;
 83                 temp.next = list2;
 84             }
 85             temp = temp.next;
 86         }
 87         
 88         list1.val = stk.pop();            //取出栈中数据,依次放入链接后的链表中
 89         temp = list1.next;
 90         while(!stk.isEmpty()){
 91                temp.val = stk.pop();
 92             temp = temp.next;
 93         }
 94         return list1;
 95         
 96         
 97     }
 98 
 99     private static void print(ListNode n) {        //输出函数
100         System.out.println(n.val);
101         if(n.next != null){
102             print(n.next);
103         }
104     }
105 
106 }

 

posted @ 2017-08-29 20:11  XuGuobao  阅读(174)  评论(0编辑  收藏  举报