算法第三季13

203,移除链表元素

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

2.两数之和

在这里插入图片描述在这里插入图片描述

package 链表;

/**
 * https://leetcode-cn.com/problems/add-two-numbers/
 * 
 * @author MJ
 *
 */
public class _0002_两数相加 {
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		if (l1 == null) return l2;
		if (l2 == null) return l1;
		
		ListNode dummyHead = new ListNode(0);
		ListNode last = dummyHead;
		// 进位值
		int carry = 0;
		while (l1 != null || l2 != null) {
			int v1 = 0;
			if (l1 != null) {
				v1 = l1.val;
				l1 = l1.next;
			}
			int v2 = 0;
			if (l2 != null) {
				v2 = l2.val;
				l2 = l2.next;
			}
			int sum = v1 + v2 + carry;
			// 设置进位值
			carry = sum / 10;
			// sum的个位数作为新节点的值
			last.next = new ListNode(sum % 10);
			last = last.next;
		}
		
		// 检查最后的进位
		if (carry > 0) {
			// carry == 1
			last.next = new ListNode(carry);
		}
		
		return dummyHead.next;
	}
}

160.相交链表

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

86.分隔链表

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

package 链表;

/**
 * https://leetcode-cn.com/problems/partition-list/
 * 
 * @author MJ
 *
 */
public class _0086_分隔链表 {
	public ListNode partition(ListNode head, int x) {
		if (head == null) return null;
		ListNode lHead = new ListNode(0);
		ListNode lTail = lHead;
		ListNode rHead = new ListNode(0);
		ListNode rTail = rHead;
		while (head != null) {
			if (head.val < x) { // 放在lTail后面
				lTail.next = head;
				lTail = head;
			} else { // 放在rTail后面
				rTail.next = head;
				rTail = head;
			}
			head = head.next;
		}
		// 这句代码不能少
        /* 
         * 因为可能出现这样的情况:
         * 原链表倒数第N个节点A的值是>=x的,A后面所有节点的值都是<x的
         * 然后rTail.next最终其实就是A.next
         */
		rTail.next = null;
		// 将rHead.next拼接在lTail后面
		lTail.next = rHead.next;
		return lHead.next;
	}
}

234.回文链表

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

/**
 * 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) {
        if(head == null || head.next == null) return true;
        if(head.next.next == null) return head.val == head.next.val;

        //找到中间结点
        ListNode mid = middleListNode(head);
        //翻转右半部分(中间结点的右半部分)
        ListNode rHead = reverseList(mid.next);
        ListNode lHead = head;

        //从lHead,rHead触发,判断是否为回文链表
        while(rHead != null){
            if(lHead.val != rHead.val) return false;
            rHead = rHead.next;
            lHead = lHead.next;
        }
        return true;

    }
    //用快慢指针找到中间结点
    private ListNode middleListNode(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    private ListNode reverseList(ListNode head){
        ListNode newHead = null;
        while(head != null){
            ListNode tmp = head.next;
            head.next = newHead;
            newHead = head;
            head = tmp;
        }
        return newHead;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未来程序员A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值