关闭

【Leetcode】Remove Nth Node From End of List

316人阅读 评论(0) 收藏 举报
分类:

题目链接:https://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 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.

思路:

1、先求链表长度,遍历链表找到要删除结点需要的指针,需要遍历两遍。

2、用两个指针,两指针距离为n,并且每次移动一步,直到表尾,此时前一个指针指向的就是倒数第n个结点

算法1:

public int getListLength(ListNode head) {
		int length = 0;
		ListNode p = head;
		while (p != null) {
			length++;
			p = p.next;
		}
		return length;
	}

	public ListNode removeNthFromEnd(ListNode head, int n) {
		int length = getListLength(head);
		ListNode pre = null, p = head;
		int i = length - n + 1;// 正序移动距离
		while (--i > 0) {// 获取要删除节点的指针
			pre = p;
			p = p.next;
		}
		if (pre != null) {
			pre.next = p.next;
		} else {
			head = p.next;// 当删除头节点时
		}
		return head;
	}


算法2:

	public ListNode removeNthFromEnd(ListNode head, int n) {
		// nth指向p前n个结点,pre为nth前一个结点,即pre/nth/p
		ListNode p = head, nth = head, pre = null;
		while (--n > 0) {
			p = p.next;
		}
		while (p.next != null) {
			p = p.next;
			pre = nth;
			nth = nth.next;
		}
		// 此时p指向表尾,nth指向倒数n个结点
		if(pre==null){ //如果删除的是头结点
			head = nth.next;
		}else{
			pre.next = nth.next;
		}
		return head;
	}


算法3:

考虑上面的算法需要考虑到如果删除的是表头,需要单独处理,经常在别人的题解中见到头结点可以将表头当普通结点一样操作,简化程序。

下面的代码就是加了头结点。

	public ListNode removeNthFromEnd(ListNode head, int n) {
		// nth指向p前n个结点,pre为nth前一个结点,即pre/nth/p
		ListNode newHead = new ListNode(0);// 头结点
		ListNode p = head, nth = head, pre = newHead;
		newHead.next = head;
		while (--n > 0) {
			p = p.next;
		}
		while (p.next != null) {
			p = p.next;
			pre = nth;
			nth = nth.next;
		}
		// 此时p指向表尾,nth指向倒数n个结点
		pre.next = nth.next;
		return newHead.next;
	}



1
0
查看评论

Remove Nth Node From End of List -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/  这道题是链表基本操作,主要问题就是如何得到链表的倒数第n个结点,应该是一个比较重要的routine,因为很多题目都要用这个subrou...
  • linhuanmars
  • linhuanmars
  • 2014-02-23 23:39
  • 5899

LeetCode 19 : Remove Nth Node From End of List (Java)

解题思路:一开始我的做法是先遍历一遍取得长度,然后长度减去n就知道要删除正着数第几个元素,结果题目要求one pass。那就只能用两个指针,一个快指针先走n步,一个慢指针从头开始走,这样当快指针走到尾部的时候,慢指针所指的就是要删除元素的前一个元素。/** * Definition for sin...
  • changetocs
  • changetocs
  • 2015-12-01 20:02
  • 951

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

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 19:26
  • 2284

LeetCode19——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, a...
  • booirror
  • booirror
  • 2015-02-05 15:07
  • 1162

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...
  • yao_wust
  • yao_wust
  • 2014-11-18 10:08
  • 3923

[leetcode] 19. Remove Nth Node From End of List python实现【easy】

Remove Nth Node From End of List My Submissions QuestionEditorial Solution Given a linked list, remove the nth node from the end of list and return...
  • zl87758539
  • zl87758539
  • 2016-06-14 21:50
  • 687

【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】

【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a linked list, remove the nth node from the end of li...
  • DERRANTCM
  • DERRANTCM
  • 2015-07-22 07:24
  • 2148

(Java)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->...
  • u012848330
  • u012848330
  • 2016-05-31 00:00
  • 290

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

19. Remove Nth Node From End of List My Submissions Question Total Accepted: 94847 Total Submissions: 330667 Difficulty...
  • bruce128
  • bruce128
  • 2016-02-20 14:58
  • 832

【LeetCode】19 Remove Nth Node From End of List (c++实现)

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-&g...
  • styshoo
  • styshoo
  • 2015-07-22 10:29
  • 372
    个人资料
    • 访问:569128次
    • 积分:8678
    • 等级:
    • 排名:第2674名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论