一天一道LeetCode系列

(一)题目

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.

(二)解题

剑指上面的经典题目了。两个指针,一个指向head,一个指向正数第K-1个,同时移动,知道第K-1的指针指向NULL。则指向head的数就是倒数第K个。

删除节点的时候要区分头节点和其他节点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(n == 0) return head;
        ListNode* ptemp = head;
        for(int i = 0 ; i < n ;i++)
        {
            ptemp = ptemp->next;
        }
        ListNode* pNth = head;
        ListNode* pre = head;
        while(ptemp!= NULL)
        {
            pre = pNth;
            ptemp = ptemp->next;
            pNth = pNth->next;
        }
        if(pNth == head)//如果是头节点
        {
            head = head->next;
        }
        else pre->next = pNth->next;//非头节点
        free(pNth);
        return head;
    }
};

【一天一道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

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

  3. 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 ...

  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 [Difficulty: Medium]

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

  8. [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 ...

  9. [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 ...

  10. 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. PGM:贝叶斯网的参数估计2

    http://blog.csdn.net/pipisorry/article/details/52599321 没时间看了,下次再看... 具有共享参数的学习模型 全局参数共享 局部参数共享 具有 共 ...

  2. Android事件分发传递回传机制详解

    转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:点击打开链接   http://blog.csdn.net/qq_32059827/article/details/5257701 ...

  3. 源码篇——AsyncTask机制

    AsyncTask new AsyncTask<String,String,String>(){ // 运行在主线程中,做预备工作 onPreExecute(){ } // 运行在子线程中 ...

  4. 磁盘管理,磁盘挂在mount,挂载光盘镜像文件,挂在U盘,umount 卸载命令, dd

    1 mount 命令格式: mount[-t vfstype] -o options device dir 其中: *-t vfstype 指定文件系统的类型,通常不必指定.mount会自动选择正确的 ...

  5. Tomcat中的ssl安全信道的实现

    为了实现https协议通信,tomcat需要利用JSSE把SSL/TLS协议集成到自身系统上,通过上一节我们知道不同的厂商可以实现自己的JSSE,而tomcat默认使用的是以前sun公司开发实现的包而 ...

  6. 用Maven打包成EAR部署JBoss

    基于原理的架构里面,考虑这次升级版本,可谓是一步一个脚印的向上走啊,可以说步步为坎,别人的知识,和自己的知识,相差很多啊,什么都懂点,但是具体没有使用,就理解不深刻了,心有余而力不足,所以一切我们自己 ...

  7. 删除表中重复行SQL

    delete from table_name a where rowid < (select max(rowid) from table_name b where a.col1 = b.col1 ...

  8. Sharing The Application Tier File System in Oracle E-Business Suite Release 12.2

    The most current version of this document can be obtained in My Oracle Support Knowledge Document 13 ...

  9. 如何通过网络连接进行ADB调试

    点击打开链接 大家在使用adb调试Android系统时可能会遇到麻烦,比如usb端口只有一个,如果用作adb调试,就不能通过usb连接其它器件,或者usb端口不能使用时也没法进行adb调试. Andr ...

  10. scala学习笔记3(trait)

    // trait 类似于 Java8 中可以带 default method 的接口. // trait 中可以带有实现的方法,也可以带有抽象的方法,使用 trait 的方式是 with 而混入类中 ...