LeeCode打卡第十七天

LeeCode打卡第十七天

第一题:合并两个有序链表(LeeCode第21题):

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。


主要思想:将两个链表从头开始比较,将两个链表中的较小值存入新链表中,比较到最后,有一个链表会先为空,此时需要一个判断函数,找到不为空的链表,将剩下的元素存入。

/**
 * 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 boolean isPalindrome(ListNode head) {
        List<Integer> list = new ArrayList<>();
        ListNode p = head;
        int n = 0;
        while(p != null){
            list.add(p.val);
            p = p.next;
        }
        
        int start = 0;
        int end = list.size() - 1;
        for(; start < end;){
            if(list.get(start++) != (list.get(end--))) return false;
        }        
        return true;
    }
}

第二题:两数相加(LeeCode第2题):

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个数都不会以 0 开头。


主要思想:最核心的点在新节点的创建与进位符的保存上/p>

/**
 * 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 dummyhead = new ListNode(-1);
        ListNode cur = dummyhead;
        ListNode p = l1, q = l2;
        int flag = 0;
        while(p != null || q != null){
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = flag + x + y;
            flag = sum / 10;
            cur.next = new ListNode(sum % 10);
            cur = cur.next;
            if(p != null) p = p.next;
            if(q != null) q = q.next;
        }
        if(flag > 0){
            cur.next = new ListNode(1);
        }
        return dummyhead.next;
    }
}

第三题:删除链表的倒数第N个结点(LeeCode第19题):

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。


主要思想:用到了双指针,因为要删除倒数第n个节点,首先需要找到倒数第n+1个节点的位置,用cur .next = cur.next.next的操作即可删除。让一个指针比第二个指针先走n+1步,这样当一个指针遍历到链表最后的时候,另一个指针刚好在倒数第n+个元素那里。

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {

        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(fast == slow) {
                ListNode p = head;
                ListNode q = slow;
                while(p != q){
                    p = p.next;
                    q = q.next;
                }
                 return p;
            }
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值