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

题目来源:

https://leetcode.com/problems/remove-nth-node-from-end-of-list/



题意分析:

  这道题是给定一个链表,删除倒数第n个节点。提醒,1.输入的链表长度必然大于n,2.尽量通过访问一次就得到结果。



题目思路:

  这道题的问题在于如何找到倒数第n个节点。由于只能访问一次,所以可以建立两个链表,tmp1和tmp2。tmp1先访问第一个到n个节点。这时候,tmp2从0开始访问。等tmp1访问结束的时候,tmp2刚好访问到倒数第n个节点。



代码(python):

 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         ans = ListNode(0);
15         ans.next = head
16         tmp1 = ans
17         tmp2 = ans
18         i = 0
19         while i < n:
20             tmp1 = tmp1.next
21             i += 1
22         while tmp1.next:
23             tmp1 = tmp1.next
24             tmp2 = tmp2.next
25         tmp2.next = tmp2.next.next
26         return ans.next
27         



转载请注明出处:http://www.cnblogs.com/chruny/p/4844007.html

时间: 2024-10-13 07:27:32

[LeetCode]题解:019-Remove Nth Node From End of List的相关文章

LeetCode题解之Remove Nth Node From End of List

1.题目描述 2.问题分析 直接计算,操作. 3.代码 1 ListNode* removeNthFromEnd(ListNode* head, int n) { 2 if (head == NULL) 3 return head; 4 int len = 0; 5 ListNode *p = head; 6 while (p != NULL) { 7 len++; 8 p = p->next; 9 } 10 11 int step = len - n; 12 if (step == 0) 13

Leetcode 线性表 Remove Nth Node From End of List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Nth Node From End of List Total Accepted: 12160 Total Submissions: 41280 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1

LeetCode 019 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. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list bec

[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

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 019.Remove_Nth_Node_From_End_of_List (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 代码(github):https://github.com/illuz/leet

Java for LeetCode 019 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[Linked 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. 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 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

【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. 首先容易想到的是

【LeetCode OJ】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. struct Li