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;
    }
}
相关推荐
沐苏瑶3 小时前
Java 搜索型数据结构全解:二叉搜索树、Map/Set 体系与哈希表
java·数据结构·算法
ZoeJoy84 小时前
算法筑基(二):搜索算法——从线性查找到图搜索,精准定位数据
算法·哈希算法·图搜索算法
Alicx.4 小时前
dfs由易到难
算法·蓝桥杯·宽度优先
_日拱一卒4 小时前
LeetCode:找到字符串中的所有字母异位词
算法·leetcode
云泽8084 小时前
深入 AVL 树:原理剖析、旋转算法与性能评估
数据结构·c++·算法
Wilber的技术分享5 小时前
【LeetCode高频手撕题 2】面试中常见的手撕算法题(小红书)
笔记·算法·leetcode·面试
邪神与厨二病5 小时前
Problem L. ZZUPC
c++·数学·算法·前缀和
梯度下降中7 小时前
LoRA原理精讲
人工智能·算法·机器学习
IronMurphy7 小时前
【算法三十一】46. 全排列
算法·leetcode·职场和发展
czlczl200209257 小时前
力扣1911. 最大交替子序列和
算法·leetcode·动态规划