【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 ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}

};
ListNode *removeNthFromEnd(ListNode *head, int n)
{
	ListNode *r = head, *s;
	if (head == NULL)
		return head;
	int i = 0;
	for (ListNode *p = head; p != NULL; p = p->next)
	{
		i++;
	}
	if (i == n)  //删除第一个节点
	{
		ListNode *l = head->next;
		free(head);
		return l;
	}
	for (int num = 0; num < i - n - 1; num++)//找到要删节点的前一个节点
	{
		r = r->next;
	}
	ListNode *tmp = r->next;
	r->next = r->next->next;
	free(tmp);
	return head;

}

时间: 2024-10-13 03:54:40

【LeetCode OJ】Remove Nth Node From End of List的相关文章

【LeetCode OJ】Remove Element

题目:Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 代码: class Solution { public: int removeElement(int A[]

【LeetCode OJ】Remove Duplicates from Sorted Array

题目:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A

【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 OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

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】Remove Nth Node From End of List (2 solutions)

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][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