LeetCode每日三题(四)链表

一、两数相加

自己答案:

java 复制代码
/**
 * 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 addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pA=l1;
        ListNode pB=l2;
        int number1=0;
        int number2=0;
        ListNode result=new ListNode(-1);
        ListNode temp=result;
        boolean flag=false; //进位标记
        while(pB!=null||pA!=null){
            if(pB==null){
                number2=0;
                number1=pA.val;
                pA=pA.next;
            } else if (pA==null) {
                number1=0;
                number2= pB.val;
                pB=pB.next;
            }else{
                number1=pA.val;
                number2=pB.val;
                pB=pB.next;
                pA=pA.next;
            }
            int sum=0;
            if(flag){
                sum = number2 + number1+1;
            }else{
                sum = number2 + number1;
            }
            if(sum>=10){
                flag=true;//开启标记
                ListNode newnode = new ListNode(sum%10);
                temp.next=newnode;
                temp=temp.next;
            }else {
                flag=false;
                ListNode newnode = new ListNode(sum);
                temp.next=newnode;
                temp=temp.next;
            }
        }
        if(flag){
            ListNode newnode=new ListNode(1);
            temp.next=newnode;
            temp=temp.next;
        }
        return result.next;
    }
}

标准答案:

java 复制代码
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }
}

二、删除链表指定结点

自己答案:

java 复制代码
/**
 * 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 removeNthFromEnd(ListNode head, int n) {
        ListNode result=new ListNode();
        ListNode temp=result;
        while(head!=null){
            if(check(head,n)){
                //没有抵达最后n个的位置
                temp.next=head;
                temp=temp.next;
                head=head.next;
            }else {
                //抵达了最后n个的位置
                //跳过倒数第n个
                head=head.next;
                temp.next=head; 
                break;
            }
        }
        return result.next;
    }

    public  boolean check(ListNode node,int n){
    int count=n;
    ListNode temp=node;
    while(count>0){
        count--;
        temp=temp.next;
    }
    if(temp==null){
        return false;
    }else return true;
    }
    
}

标准答案:

双指针:

java 复制代码
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        ListNode first = head;
        ListNode second = dummy;
        for (int i = 0; i < n; ++i) {
            first = first.next;
        }
        while (first != null) {
            first = first.next;
            second = second.next;
        }
        second.next = second.next.next;
        ListNode ans = dummy.next;
        return ans;
    }
}

栈:

java 复制代码
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        Deque<ListNode> stack = new LinkedList<ListNode>();
        ListNode cur = dummy;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        for (int i = 0; i < n; ++i) {
            stack.pop();
        }
        ListNode prev = stack.peek();
        prev.next = prev.next.next;
        ListNode ans = dummy.next;
        return ans;
    }
}

知识点:

三、两两交换链表中的结点

自己答案:

java 复制代码
/**
 * 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 swapPairs(ListNode head) {
        ListNode result=new ListNode(-1,head);
        ListNode cur=result;
        while((cur.next!=null)&&(cur.next.next!=null)){
            //两两交换 A B C
            //结点A 与 B交换
            //先记录A地址 和 B后面的C的地址
            ListNode temp1=cur.next;
            ListNode temp2=cur.next.next.next;
            //指向B
            cur.next=temp1.next;
            //B指向A
            cur.next.next=temp1;
            //A指针 = B的指针
            temp1.next=temp2;

            cur=cur.next.next;

        }
        return result.next;
    }
}

标准答案:

java 复制代码
class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = head.next;
        head.next = swapPairs(newHead.next);
        newHead.next = head;
        return newHead;
    }
}
相关推荐
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
moonlifesudo1 天前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法