编程题_逆置链表、合并有序链表

1.逆置链表

在这里插入图片描述

class Node {
    int value;
    Node next;
}

public class Main {

    public static Node reverse(Node head) {
        // 遍历,把每个结点头插到新链表中

        // 指向新链表的第一个结点
        Node result = null;
        Node cur = head;
        while (cur != null) {
            Node next = cur.next;
            
            // 把 cur 头插到新链表中
            cur.next = result;
            result = cur;
            
            cur = next;
        }

        return result;
    }

    public static Node reverse2(Node head) { //递归思想     
    
        if (head == null || head.next == null) {//空链表或只有一个结点
            return head;
        }

        Node result = reverse2(head.next);
        head.next.next = head;
        head.next = null;
        return result;
    }
}

2.合并有序链表

在这里插入图片描述
思路:将原链表中结点的值进行比较 数值较小的尾插到新链表上
merge1:常规方法 注意链表为空的情况
merge2:新链表开始就有一个结点 好处是不用处理新链表尾插时新链表为空的情况 注意最后返回的是result.next

class Node {
    int value;
    Node next;
}
public class Main {
    public static Node merge(Node head1, Node head2) {
        if (head1 == null) {
            return head2;
        }
        if (head2 == null) {
            return head1;
        }

        Node cur1 = head1;
        Node cur2 = head2;
        Node result = null;
        Node last = null;  //尾插是最后一个结点记为last

        while (cur1 != null && cur2 != null) {
            if (cur1.value <= cur2.value) {  //cur1尾插到result
                if (result == null) {
                    result = cur1;
                } else {
                    last.next = cur1;
                }
                last = cur1;
                cur1 = cur1.next;
            } else {  //cur2尾插到result
                if (result == null) {
                    result = cur2;
                } else {
                    last.next = cur2;
                }
                last = cur2;
                cur2 = cur2.next;
            }
        }

        if (cur1 != null) {
            last.next = cur1;
        } else {
            last.next = cur2;
        }

        return result;
    }

    public static Node merge2(Node head1, Node head2) {
        Node cur1 = head1;
        Node cur2 = head2;

        Node result = new Node();
        Node last = result;   //不用处理新链表尾插为空的情况 不用分情况讨论
        
        while (cur1 != null && cur2 != null) {
            if (cur1.value <= cur2.value) {
                last.next = cur1;
                cur1 = cur1.next;
            } else {
                last.next = cur2;
                cur2 = cur2.next;
            }
            last = last.next;
        }

        if (cur1 != null) {
            last.next = cur1;
        } else {
            last.next = cur2;
        }

        return result.next;  //注意  返回result.next
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值