码迷,mamicode.com
首页 > 编程语言 > 详细

Java 合并两个有序链表

时间:2017-10-13 11:14:44      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:class   list   null   value   order   java   ==   package   非递归   

非递归:

package cookie;

public class MergeOrderedList {
    public Node Merge(Node head1, Node head2) {
        if (head1 == null)
            return head2;
        if (head2 == null)
            return head1;
        Node head = null;
        if (head1.value < head2.value) {
            head = head1;
            head1 = head1.next;
        } else {
            head = head2;
            head2 = head2.next;
        }
        Node temp = head;
        while (head1 != null && head2 != null) {
            if (head1.value < head2.value) {
                temp.next = head1;
                head1 = head1.next;
            } else {
                temp.next = head2;
                head2 = head2.next;
            }
        }
        return head;
    }
}

class Node {
    public int value;
    public Node next;
}

递归,《剑指offer》116页

Java 合并两个有序链表

标签:class   list   null   value   order   java   ==   package   非递归   

原文地址:http://www.cnblogs.com/chenhuanBlogs/p/7660042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!