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 n will always be valid.
Try to do this in one pass.

解题思路:

难度不大,注意下边界条件即可:

JAVA实现:

static public ListNode removeNthFromEnd(ListNode head, int n) {
		if(n<=0)
			return head;
		ListNode ln=head;
		int i=1;
		while(ln.next!=null){
			ln=ln.next;
			i++;
		}
		if(i==n)
			return head.next;
		ln=head;
		for(;i>n+1;i--)
			ln=ln.next;
		ln.next=ln.next.next;
		return head;
	}
时间: 2024-10-26 01:21:40

Java for LeetCode 019 Remove Nth Node From End of List的相关文章

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

[Leetcode][019] Remove Nth Node From End of List (Java)

题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointers [个人分析] 这个题目应该算是Linked List里面的基础题.说它基础不是因为它简单,而是因为它是基石.一些 Linked list中经典方法在这道题里面都有应用. 1. Two Pointers 在 Linked List中: 如果能预先链表知道长度,那题目就简单多了,删掉从头开始的 Le

LeetCode:Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

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.Note:Give

[LeetCode][JavaScript]Remove Nth Node From End of List

https://leetcode.com/problems/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

【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 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