[19] 删除链表的倒数第 N 个结点
This commit is contained in:
parent
46cd9f1348
commit
4ee6afd885
|
|
@ -5,7 +5,7 @@ package leecode;/*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// @lc code=start
|
// @lc code=start
|
||||||
class Solution {
|
class No122 {
|
||||||
//贪心算法
|
//贪心算法
|
||||||
public int maxProfit(int[] prices) {
|
public int maxProfit(int[] prices) {
|
||||||
int len = prices.length;
|
int len = prices.length;
|
||||||
|
|
@ -94,7 +94,7 @@ class Solution {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int[] price = new int[]{1, 2, 3};
|
int[] price = new int[]{1, 2, 3};
|
||||||
Solution solution = new Solution();
|
No122 solution = new No122();
|
||||||
System.out.println(solution.maxProfit(price));
|
System.out.println(solution.maxProfit(price));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class leecode.Solution:
|
||||||
def longestCommonPrefix(self, strs: List[str]) -> str:
|
def longestCommonPrefix(self, strs: List[str]) -> str:
|
||||||
# 遍历
|
# 遍历
|
||||||
# if not strs:
|
# if not strs:
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ class ListNode:
|
||||||
self.next = next
|
self.next = next
|
||||||
|
|
||||||
|
|
||||||
class Solution:
|
class leecode.Solution:
|
||||||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
|
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
|
||||||
if not list1:
|
if not list1:
|
||||||
return list2 # 终止条件,直到两个链表都空
|
return list2 # 终止条件,直到两个链表都空
|
||||||
|
|
@ -29,5 +29,5 @@ class Solution:
|
||||||
return list2
|
return list2
|
||||||
|
|
||||||
|
|
||||||
sol = Solution()
|
sol = leecode.Solution()
|
||||||
# @lc code=end
|
# @lc code=end
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
# 奇数位 121
|
# 奇数位 121
|
||||||
# 12 1
|
# 12 1
|
||||||
# 1 12
|
# 1 12
|
||||||
class Solution:
|
class leecode.Solution:
|
||||||
def isPalindrome(self, x: int) -> bool:
|
def isPalindrome(self, x: int) -> bool:
|
||||||
if x < 0 or (x % 10 == 0 and x != 0):
|
if x < 0 or (x % 10 == 0 and x != 0):
|
||||||
return False
|
return False
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue