链表的一些算法

来源:互联网 发布:java多线程编程面试题 编辑:程序博客网 时间:2024/06/07 05:31

已知两个链表head1 和head2各自有序,请把他们合并成一个依然有序的链表。结果就是要包含head1和head2的所有节点,即节点值相同。

本题我采用的递归方法,思路步骤如下:
1. 比较链表1和建表2的第一个节点的数据,如果head1.data

package cn.exercise.algorithmsTest;class Node {    Node next = null;    int data;    public Node(int data) {        this.data = data;    }}public class MergeList {    public static Node mergeList(Node head1, Node head2) {        if (head1 == null) {            return head2;        }        if (head2 == null) {            return head1;        }        Node head = null;        if (head1.data < head2.data) {            head = head1;            head.next = mergeList(head1.next, head2);        } else {            head = head2;            head.next = mergeList(head1, head2.next);        }        return head;    }    public static void main(String[] args) {        // 创建两个链表 1,3,5 和2,4,6        Node head1 = new Node(1);        Node node3 = new Node(3);        head1.next = node3;        Node node5 = new Node(5);        node3.next = node5;        node5.next = null;        Node head2 = new Node(2);        Node node4 = new Node(4);        head2.next = node4;        Node node6 = new Node(6);        node4.next = node6;        node6.next = null;        Node mergeHead = mergeList(head1, head2);        while (mergeHead != null) {            System.out.print(mergeHead.data + " ");            mergeHead = mergeHead.next;        }    }}打印结果1 2 3 4 5 6 

将两个链表生成相加链表。即对应的位置相加

package cn.exercise.algorithmsTest;import java.util.Stack;class Node1 {    int data;    Node1 next;    public Node1(int data) {        this.data = data;    }}public class LinkedListAdd {    /**     * 两个单链表生成相加链表     */    public Node1 addList2(Node1 head1, Node1 head2) {        Stack<Integer> stack1 = new Stack<Integer>();        Stack<Integer> stack2 = new Stack<Integer>();        while (head1 != null) {            stack1.push(head1.data);            head1 = head1.next;        }        while (head2 != null) {            stack2.push(head2.data);            head2 = head2.next;        }        int n1 = 0;// 链表1的数值        int n2 = 0;// 链表2的数值        int n = 0;// n1+n2+ca//      int ca = 0;// 进位        Node1 node1 = null;// 当前节点        Node1 pNode1 = null;// 当前节点的前驱节点        while (!stack1.isEmpty() || !stack2.isEmpty()) {            n1 = stack1.isEmpty() ? 0 : stack1.pop();            n2 = stack2.isEmpty() ? 0 : stack2.pop();            n = n1 + n2;            node1 = new Node1(n);            node1.next = pNode1;            pNode1 = node1;//          ca = n / 10;        }//      if (ca == 1) {//          //          node1 = new Node1(n / 10);//          node1.next = pNode1;//          pNode1 = node1;//      }        return node1;    }    public static void main(String[] args) {        // 创建两个链表 1,3,5 和2,4,6        Node1 head1 = new Node1(1);        Node1 Node13 = new Node1(3);        head1.next = Node13;        Node1 Node15 = new Node1(5);        Node13.next = Node15;        Node15.next = null;        Node1 head2 = new Node1(2);        Node1 Node14 = new Node1(4);        head2.next = Node14;        Node1 Node16 = new Node1(6);        Node14.next = Node16;        Node16.next = null;        Node1 list = new LinkedListAdd().addList2(head1,head2);        while (list != null) {            System.out.print(list.data + " ");            list = list.next;        }    }}打印结果3 7 11