算法刷题:链表三题(2)

1.链表相交

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

解析

先获取两个链表的长度,然后移动长链表的指针次数为两个链表长度的差,最后一起移动两个链表的指针并比较是否相同,相同则返回相同的节点,到底都没有相同节点,则返回null。时间复杂度发O(n),空间复杂度O(1)。

代码

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int indexA=0;
       int indexB=0;
        ListNode curA=headA;
        ListNode curB=headB;
        while (curA!=null){
            indexA++;
            curA=curA.next;
        }
        while (curB!=null){
            indexB++;
            curB=curB.next;
        }
        curA= indexA>indexB?headA:headB;
        curB= curA==headA?headB:headA;
        int big= indexA>indexB?indexA:indexB;
        int small= indexA>indexB?indexB:indexA;
        int interval= big-small;
        while (interval-->0){
            curA=curA.next;
        }
        while (curA!=null&&curB!=null){
            if (curA==curB)
                return curA;
            curA=curA.next;
            curB=curB.next;
        }


        return null;
    }

2.两数相加

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

代码

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
              Stack<Integer> listNodes1 = new Stack<>();
        Stack<Integer> listNodes2 = new Stack<>();
        Stack<Integer> jg = new Stack<>();

        while (l1 != null) {
            listNodes1.add(l1.val);
            l1 = l1.next;
        }
        while (l2 != null) {
            listNodes2.add(l2.val);
            l2 = l2.next;
        }
        int ident = 0;
        while (listNodes1.size() != 0 || listNodes2.size() != 0) {

            int i = (listNodes1.size() == 0 ? 0 : listNodes1.pop()) + (listNodes2.size() == 0 ? 0 : listNodes2.pop());
            if (i > 9) {
                jg.add((i + ident) % 10);
                ident = 1;

            } else {
                jg.add((i + ident) % 10);
                if (i + ident<10){
                    ident=0;
                }
            }
        }
        if (ident == 1) {
            jg.add(1);
        }
        ListNode listNode = new ListNode(jg.pop());
        ListNode l3 = listNode;
        while (jg.size() != 0) {
            l3.next = new ListNode(jg.pop());
            l3 = l3.next;
        }

        return listNode;
    }

3.反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

代码

public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        ListNode newHead = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            if (curNext == null) {
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return newHead;
    }    

题目来源:力扣(LeetCode)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值