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;
    }
}
相关推荐
f狐0狸x18 分钟前
【蓝桥杯每日一题】4.1
c语言·c++·算法·蓝桥杯
ん贤18 分钟前
2023第十四届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组(真题&题解)(C++/Java题解)
java·c语言·数据结构·c++·算法·蓝桥杯
梭七y20 分钟前
【力扣hot100题】(022)反转链表
算法·leetcode·链表
威视锐科技3 小时前
软件定义无线电36
网络·网络协议·算法·fpga开发·架构·信息与通信
牧歌悠悠4 小时前
【Python 算法】动态规划
python·算法·动态规划
JINX的诅咒4 小时前
CORDIC算法:三角函数的硬件加速革命——从数学原理到FPGA实现的超高效计算方案
算法·数学建模·fpga开发·架构·信号处理·硬件加速器
明天不下雨(牛客同名)5 小时前
为什么 ThreadLocalMap 的 key 是弱引用 value是强引用
java·jvm·算法
lisw055 小时前
DeepSeek原生稀疏注意力(Native Sparse Attention, NSA)算法介绍
人工智能·深度学习·算法
喝拿铁写前端6 小时前
SmartField AI:让每个字段都找到归属!
前端·算法
莫有杯子的龙潭峡谷6 小时前
3.31 代码随想录第三十一天打卡
c++·算法