[19] 删除链表的倒数第 N 个结点

This commit is contained in:
Wen 2023-04-26 15:44:38 +08:00
parent 46cd9f1348
commit 4ee6afd885
5 changed files with 45 additions and 6 deletions

View File

@ -5,7 +5,7 @@ package leecode;/*
*/
// @lc code=start
class Solution {
class No122 {
//贪心算法
public int maxProfit(int[] prices) {
int len = prices.length;
@ -94,7 +94,7 @@ class Solution {
public static void main(String[] args) {
int[] price = new int[]{1, 2, 3};
Solution solution = new Solution();
No122 solution = new No122();
System.out.println(solution.maxProfit(price));
}

View File

@ -0,0 +1,39 @@
package leecode;/*
* @lc app=leetcode.cn id=19 lang=java
*
* [19] 删除链表的倒数第 N 个结点
*/
// @lc code=start
// Definition for singly-linked list.
import java.util.ArrayList;
import java.util.List;
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
List<ListNode> cache =new ArrayList<>();
ListNode ret = head;
while (head != null) {
cache.add(head);
head = head.next;
}
if (n == cache.size()) {
return ret.next;
}
cache.get(cache.size() - n - 1).next = n == 1 ? null : cache.get(cache.size() - n + 1);
return ret;
}
// private static class ListNode {
// int val;
// ListNode next;
// ListNode() {}
// ListNode(int val) { this.val = val; }
// ListNode(int val, ListNode next) { this.val = val; this.next = next; }
// }
}
// @lc code=end

View File

@ -9,7 +9,7 @@
from typing import List
class Solution:
class leecode.Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
# 遍历
# if not strs:

View File

@ -15,7 +15,7 @@ class ListNode:
self.next = next
class Solution:
class leecode.Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not list1:
return list2 # 终止条件,直到两个链表都空
@ -29,5 +29,5 @@ class Solution:
return list2
sol = Solution()
sol = leecode.Solution()
# @lc code=end

View File

@ -13,7 +13,7 @@
# 奇数位 121
# 12 1
# 1 12
class Solution:
class leecode.Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0 or (x % 10 == 0 and x != 0):
return False