[LeetCode]#19 Remove Nth Node From the End of list

从移动回来一个月,就一直在忙项目申请书的事情。通过写申请书,看新闻联播的新闻稿都开始顺眼了。

师兄师姐们已经开始找工作了,我还有一年时间,工作方面早作准备。机器学习方面继续推导常见的算法,刷一刷kaggle,做做特征工程什么的,算是应用了。

今天这道题其实并不难,但我是在Linux下写的,有些本地调试的方法和技巧想记录一下。

一、题目

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

二、解析

一个链表,删除倒数第n个结点,最好用一次遍历的方法。

第一反应当然是遍历一遍链表,看一下长度是多少;然后再遍历链表,删除倒数第n个。

看到一次遍历后,就决定用空间换时间,开一个list存结点的地址,这样只需要在list里用两三步就能实现节点的删除

三、代码

 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution(object):
 8     def removeNthFromEnd(self, head, n):
 9         """
10         :type head: ListNode
11         :type n: int
12         :rtype: ListNode
13         """
14         address = []
15         while head:
16             address.append(head)
17             head = head.next
18
19         length = len(address)
20         if length == 0:
21             return address[0]
22         elif length == 1:
23             if n == 0:
24                 return address[0]
25             elif n == 1:
26                 return []
27         else:
28             if n == 0:
29                 return address[0]
30             elif length == n:
31                 return address[1]
32             else:
33                 target = address[length - n]
34                 p = target.next
35                 address[length - n - 1].next = p
36                 del(address[length - n])
37                 return address[0]

四、收获

1.前两天装了CentOS,用VIM写这个程序。在本地调试的时候,遇到了麻烦,平时没怎么写过Class,突然一下就不会调试了。经过恪爷指导,通过零星的一点知识储备,最终还是搞定了。

程序结构应该是这样的:

 1 ###########q19.py###############
 2 Class ListNode(object):
 3     def __init__(self, x):
 4         self.val = x
 5         self.next = None
 6
 7 Class Solution(object):
 8     def removeNthFromEnd(self, head, n):
 9         ....
10         return head
11
12     def main(self):
13         #不同类可以直接调用于声明对象
14         head = LinkList(2)
15
16         #在类中,调用同类方法要用self.function()
17         linklist = self.removeNthFromEnd(head, 3)
18
19         return linklist
20
21 sl = Solution()
22 sl.main()
23
24 ##############终端调试############
25 import q19
26 sl = q19.Solution()
27 linklist = q19.main()
28
29
30 其实是不需要Solution中def main(),直接sl.removeNthFromEnd也是可以调用函数的,但是在main中还是要方便一些。
时间: 2024-08-13 02:19:58

[LeetCode]#19 Remove Nth Node From the End of list的相关文章

leetCode 19. Remove Nth Node From End of List 链表

19. Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. For example,    Given linked list: 1->2->3->4->5, and n = 2.    After removing the second node from the end, the linked lis

leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法

Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes

Leetcode 19 Remove Nth Node From End of List (快慢指针)

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

LeetCode 19. Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

leetcode 19. Remove Nth Node From End of List(链表)

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

Leetcode 19. Remove Nth Node From End of List JAVA语言

Given a linked list, remove the nth node from the end of list and return its head. For example,    Given linked list: 1->2->3->4->5, and n = 2.    After removing the second node from the end, the linked list becomes 1->2->3->5. Note:

Java [leetcode 19]Remove Nth Node From End of List

题目描述: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:

LeetCode 19 Remove Nth Node From End of List (C,C++,Java,Python)

Problem: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Not

LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点

题目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Gi