leetcode 【 Remove Nth Node From End of List 】 python 实现

题目

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.

代码:oj在线测试通过 Runtime: 188 ms

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution:
 8     # @return a ListNode
 9     def removeNthFromEnd(self, head, n):
10         if head is None:
11             return head
12
13         dummyhead = ListNode(0)
14         dummyhead.next = head
15         p1 = dummyhead
16         p2 = dummyhead
17
18         for i in range(0,n):
19             p1 = p1.next
20
21         while p1.next is not None:
22             p1 = p1.next
23             p2 = p2.next
24             if p1.next is None:
25                 break
26
27         p2.next = p2.next.next
28
29         return dummyhead.next

思路

Linked List基本都需要一个虚表头,这道题主要思路是双指针

让第一个指针p1先走n步,然后再让p1和p2一起走;当p1走到链表最后一个元素的时候,p2就走到了倒数n+1个元素的位置;这时p2.next向表尾方向跳一个。

注意下判断条件是p1.next is not None,因此在while循环中添加判断p1.next是否为None的保护判断。

再有就是注意一下special case的情况,小白我的习惯是在最开始就把这种case都判断出来;可能牺牲了代码的简洁性,有大神路过也请拍砖指点。

时间: 2024-10-11 20:53:57

leetcode 【 Remove Nth Node From End of List 】 python 实现的相关文章

[leetcode]Remove Nth Node From End of List @ Python

原题地址:http://oj.leetcode.com/problems/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 n

LeetCode: Remove Nth Node From End of List [019]

[题目] 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: G

LeetCode: Remove Nth Node From End of List 解题报告

Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Question Solution 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,

LeetCode——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 

[LeetCode]Remove Nth Node From End of List

提米:删除链表从尾部到头部第N的节点. 思路: 两个指针,一个从头开始前移N个节点后,第二个指针开始移动,当第一指针移动到末尾时,第二个指针指向的是从尾部到头部的第N个节点. 注意: 1.N不合法,大于链表长度 2.要删除的是头结点 /***************************************************************************************** Given a linked list, remove the nth node f

[LeetCode] 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] 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: Given

[Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

Given a linked list, remove the n th 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:  Give

LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *removeNthFromEnd(ListNode *head, int n) { 12 struct

[Leetcode][Python]19: Remove Nth Node From End of List

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 19: Remove Nth Node From End of Listhttps://oj.leetcode.com/problems/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 ex