题目描述:

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.

解题思路:

设置两个指针,两个指针相隔n-1,然后两个指针同时向后移动,当后一个指针没有后继节点了,那么前一个指针指向的节点就是需要删除的节点。

代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null)
return null;
ListNode pPre = null;
ListNode p = head;
ListNode q = head;
for(int i = 0; i < n - 1; i++)
q = q.next;
while(q.next != null){
pPre = p;
p = p.next;
q = q.next;
}
if(pPre == null)
return head.next;
pPre.next = p.next;
return head;
}
}

Java [leetcode 19]Remove Nth Node From End of List的更多相关文章

  1. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

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

  2. 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

  3. [leetcode 19] Remove Nth Node From End of List

    1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  4. 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, Give ...

  5. (链表 双指针) leetcode 19. Remove Nth Node From End of List

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  6. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  7. [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, Give ...

  8. [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  9. 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, Give ...

随机推荐

  1. Mac下使用sublime text 2开发Python

    入门虽易, 熟练不易, 且行且珍惜 简介:这只是简单介绍一个好的文本工具sublime text的使用,如果要获得详细的教程可以去看这本书<Sublime Productivity>和一些 ...

  2. 1078. Hashing (25)

    时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...

  3. SNMP的工作原理&软件开发

    SNMP(Simple Network Management Protocol,简单网络管理协议)首先是由IETF的研究小组为了解决Internet上的路由器管理问题而提出的.SNMP的设计原则是简单 ...

  4. SQL的表连接

    每天给自己扫盲,让自己变得越博学. 继续学习<程序员的SQL金典>,这回我们来看看表连接相关的内容.表连接的相关知识在实际的项目开发当中,使用非常广. 所谓表连接,就是通过关联多张表,从而 ...

  5. DIV中TABLE居的2种方式

    <html><head><title>测试页面</title></head><body><div width=" ...

  6. objective-c宏定义

    1.先来几个常用的: // 是否高清屏 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? ...

  7. HDU4704+费马小定理

    费马小定理题意:求s1+s2+s3+...+sn;si表示n划分i个数的n的划分的个数,如n=4,则s1=1,s2=3    利用隔板定理可知,就是求(2^n-1)%mod-----Y    现在已知 ...

  8. Oracle 学习笔记(一)

    1.连接数据库命令: conn 用户名/密码,当用特权身份连接时,要加上as sysdba 2.修改密码: passw(ord),如果要修改其他人的密码,需要用sys或者system登录 3.显示当前 ...

  9. 5.查找最小的k个元素(数组)

    题目: 输入n个整数,输出其中最小的k个,例如输入1,2,3,4,5,6,7,8这8个数,则最小的4个是1,2,3,4(输出不要求有序) 解: 利用快速排序的partition,算导上求第k大数的思想 ...

  10. PHP简单语法

    PHP简单语法 声明变量 $var_name="1"; $var_num=1; $var_bool=true; var_dump"函数可以将我们的变量的数据类型显示出来. ...