zoukankan      html  css  js  c++  java
  • leetcode21.合并两个有序链表(链表使用,递归算法)

    public class LeetCode21 {
    //链表结点
    public static class ListNode{
    int val;
    ListNode next;
    ListNode(){
    next=null;
    }
    ListNode(int x){
    val=x;
    }
    ListNode(int val,ListNode next){
    this.val=val;
    this.next=next;
    }
    }
    //递归算法
    public ListNode mergeTwoLists(ListNode l1,ListNode l2){
    if(l1==null){
    return l2;
    }
    if(l2==null){
    return l1;
    }
    if(l1.val<l2.val){
    //取消l1指针原指向,指向一个新的两个链表,具体指向等返回值
    l1.next=mergeTwoLists(l1.next,l2);
    //此返回值在递归返回时起指向作用
    return l1;
    } else{
    l2.next=mergeTwoLists(l1,l2.next);
    return l2;
    }
    /**
    * l1:1->3->5
    * l2:1->2->4
    * 第一次递归
    * l2:1->剩余
    * 第二次
    * l1:1->剩余
    * 第三次
    * l2:2->剩余
    * 第四次
    * l1:3->剩余
    * 第五次
    * l2:4->剩余
    * 第六次:l2=null;返回l1
    * 此时l2:4->l1:5
    * 再返回l2
    * 此时l1:3->l2:4
    * 再返回l1
    * 此时l2:2->l1.3
    * 再返回l2
    * 此时l1:1->l2:3
    * 再返回l1;
    * 此时l2:1->l1:1;
    * 返回l2,递归结束,最终得到一条链表,为l2:1->l1:1->l2:2->l1:3->l2:4->l1:5
    */
    }

    //迭代算法
    public ListNode twoList(ListNode l1,ListNode l2){
    //下面两行代码目的是,pre作为头节点,head作为添加结点进行添加(相关联),最终返回头节点
    ListNode pre=new ListNode();
    ListNode head=pre;
    //判断较小的数据,并将其添加到新指针下
    while (l1!=null&&l2!=null){
    if(l1.val<=l2.val){
    head.next=l1;
    l1=l1.next;
    }else {
    head.next=l2;
    l2=l2.next;
    }
    head=head.next;
    }
    //将未到null的剩余结点进行添加
    head.next=l1==null?l2:l1;
    return pre.next;
    }

    public static void main(String args[]){
    //初始节点
    ListNode node3 =new ListNode(0);
    ListNode node31=new ListNode(1);
    ListNode node=node3;
    node.next=new ListNode(1);
    node.next.next=new ListNode(2);
    node.next.next.next=new ListNode(3);
    ListNode node1=node31;
    node1.next=new ListNode(4);
    LeetCode21 leetCode21=new LeetCode21();
    ListNode node2=new ListNode();
    node2=leetCode21.mergeTwoLists(node3,node31);
    while (node2!=null){
    System.out.println(node2.val);
    node2=node2.next;
    }
    }
    }

    /**leetcode提交代码,递归算法
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode() {}
    * ListNode(int val) { this.val = val; }
    * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    * }
    */
    /**
    * class Solution {
    * public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    * if(l1==null){
    * return l2;
    * }
    * if(l2==null){
    * return l1;
    * }
    * if(l1.val<l2.val){
    * //取消l1指针原指向,指向一个新的两个链表,具体指向等返回值
    * l1.next=mergeTwoLists(l1.next,l2);
    * //此返回值在递归返回时起指向作用
    * return l1;
    * } else{
    * l2.next=mergeTwoLists(l1,l2.next);
    * return l2;
    * }
    * }
    * }
    */
  • 相关阅读:
    排序算法
    彩票随机6+1,开发实例
    synchronized关键字以及对象锁和类锁的区别
    shell基础
    easyui+Spring MVC+hibernate = 乐途
    easyui + jdbc 实现简单的数据库管理。
    js map
    struts2 using kindeditor upload pictures (including jmagic compressed images)
    easyui filebox 浏览图片
    easyui datagrid to excel
  • 原文地址:https://www.cnblogs.com/shudaixiongbokeyuan/p/13369020.html
Copyright © 2011-2022 走看看