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;
    }
}
相关推荐
一只码代码的章鱼16 分钟前
学习笔记2(Lombok+算法)
笔记·学习·算法
jerry60944 分钟前
c++流对象
开发语言·c++·算法
vim怎么退出1 小时前
43.验证二叉搜索树
前端·leetcode
2301_817031652 小时前
C语言-- 深入理解指针(4)
c语言·开发语言·算法
·醉挽清风·2 小时前
学习笔记—双指针算法—移动零
c++·笔记·学习·算法
几点才到啊2 小时前
使用 malloc 函数模拟开辟一个 3x5 的整型二维数组
数据结构·算法
编程绿豆侠3 小时前
力扣HOT100之链表:23. 合并 K 个升序链表
算法·leetcode·链表
Ayanami_Reii3 小时前
Leetcode837.新21点
c++·笔记·算法
我想进大厂3 小时前
图论---最大流(Dinic)
算法·深度优先·图论
brzhang3 小时前
效率神器!TmuxAI:一款无痕融入终端的AI助手,让我的开发体验翻倍提升
前端·后端·算法