[Lintcode]174. Remove Nth Node From End of List/[Leetcode]

174. Remove Nth Node From End of List/19. Remove Nth Node From End of List

  • 本题难度: Easy/Medium
  • Topic: Linked List

Description

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

Example

Example 1:

Input: list = 1->2->3->4->5->null, n = 2

Output: 1->2->3->5->null

Example 2:

Input: list = 5->4->3->2->1->null, n = 2

Output: 5->4->3->1->null

Challenge

Can you do it without getting the length of the linked list?

Notice

The minimum number of nodes in list is n.

我的代码

"""
Definition of ListNode
class ListNode(object):
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""

class Solution:
    """
    @param head: The first node of linked list.
    @param n: An integer
    @return: The head of linked list.
    """
    def removeNthFromEnd(self, head, n):
        # write your code here
        if head == None:
            return None
        p1, p2 = head, head
        while(n>0):
            p1 = p1.next
            n = n-1
        if p1:
            p1 = p1.next
            while(p1):
                p1 = p1.next
                p2 = p2.next
            p2.next = (p2.next).next
            return head
        else:
            return head.next

思路

(这个题我在面试时遇到过。没有答出来,被同学疯狂嘲笑了一波。

设两个指针,期间相隔n,当前一个指针到链尾时,返回另一个指针。

需要考虑的问题:

  1. 当链表为空时
  2. 除去链头元素时。
  • 时间复杂度 O(n)
  • 出错
  1. 没考虑到特殊情况
  2. 没算清移动次数,要自己画一画才知道。

原文地址:https://www.cnblogs.com/siriusli/p/10363766.html

时间: 2024-10-14 22:03:30

[Lintcode]174. Remove Nth Node From End of List/[Leetcode]的相关文章

Remove Nth Node From End of List leetcode 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: Gi

[lintcode easy]Remove Nth Node From End of List

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

Remove Nth Node From End of List leetcode

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 

19. Remove Nth Node From End of List Leetcode 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

[LintCode] Remove Nth Node From End of List

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

Merge Two Sorted Lists & Remove Nth Node From End of List

1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 2.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,

[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

63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va

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