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;
    }
}
相关推荐
被开发耽误的大厨1 天前
1、==、equals、hashCode底层原理?重写场景?
算法·哈希算法
WolfGang0073211 天前
代码随想录算法训练营 Day38 | 动态规划 part11
算法·动态规划
松☆1 天前
C++ 算法竞赛题解:P13569 [CCPC 2024 重庆站] osu!mania —— 浮点数精度陷阱与 `eps` 的深度解析
开发语言·c++·算法
jr-create(•̀⌄•́)1 天前
正则化和优化算法区别
pytorch·深度学习·神经网络·算法
li星野1 天前
刷题:数组
数据结构·算法
tankeven1 天前
HJ182 画展布置
c++·算法
CS_Zero1 天前
无人机路径规划算法——EGO-planner建模总结—— EGO-planner 论文笔记(一)
论文阅读·算法·无人机
杰梵1 天前
聚酯切片DSC热分析应用报告
人工智能·算法
@BangBang1 天前
leetcode (4): 连通域/岛屿问题
算法·leetcode·深度优先
Ulyanov1 天前
像素迷宫:路径规划算法的可视化与实战
大数据·开发语言·python·算法