LintCode之链表倒数第n个节点
题目描述:

我的代码:
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/ public class Solution {
/*
* @param head: The first node of linked list.
* @param n: An integer
* @return: Nth to last node of a singly linked list.
*/
public ListNode nthToLast(ListNode head, int n) {
// write your code here
if(head == null) {
return null;
}
ListNode h = new ListNode(-1);
//倒置单链表
while(head != null) {
ListNode node = new ListNode(head.val);
if(h.next == null) {
h.next = node;
}else {
node.next = h.next;
h.next = node;
}
if(head.next != null) {
head = head.next;
}else {
head = null;
}
}
h = h.next;
//取得倒置后的单链表的第n个节点,这个节点就是原链表的倒数第n个节点
for(int i=1; i<n; i++) {
h = h.next;
}
ListNode node = new ListNode(h.val);
return node;
}
}
总结:因为这是单链表,无法像双链表一样轻松的获得一个节点的前一个节点,所以,我就把这个单链表倒置,倒置后的单链表的第n个节点就是倒置前的单链表的倒数第n个节点,这样就能通过遍历获得倒数第n个节点了。
LintCode之链表倒数第n个节点的更多相关文章
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- lintcode :nth to Last Node In List 链表倒数第n个节点
题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...
- LintCode 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 分析:设两个指针 p1和p2 ...
- [LeetCode] 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 ...
- 删除单链表倒数第n个节点
基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...
- 13. 求链表倒数第k个节点
题目:输入1个链表,输出该链表倒数第k个节点,有头指针和尾指针.链表倒数第0个节点是链表的尾指针节点. 代码: /* 尾指针是倒数第0个,则倒数第k个是正数第len-k个,计算len */ #incl ...
- 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...
- [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 ...
- lintcode166 链表倒数第n个节点
链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...
随机推荐
- 线程局部存储tls的使用
线程局部存储(Thread Local Storage,TLS)主要用于在多线程中,存储和维护一些线程相关的数据,存储的数据会被关联到当前线程中去,并不需要锁来维护.. 因此也没有多线程间资源竞争问题 ...
- Hibernate异常:IllegalArgumentException
异常信息: java.lang.IllegalArgumentException: attempt to create delete event with null entity at org.hib ...
- vue 中公共样式
html, body, h1, h2, h3, h4, h5, h6, p, textarea, input, select, ul, ol, li, fieldset, figure { margi ...
- 同一客户端多个git账号的配置
同一客户端多个git账号的配置 同一客户端多个git账号的配置 步骤一:用ssh-keygen命令生成一组新的id_rsa_new和id_rsa_new.pub. 1 ssh-keygen -t rs ...
- LeetCode 852. Peak Index in a Mountain Array(C++)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- 在pythonanywhere部署你的第一个应用
pythonanywhere是一个免费的托管python的代码,可以测试你的web应用,用起来还是比较方便的,现在就来介绍如何在pythonanywhere部署你的应用. 下载你的代码 我的代码是托管 ...
- elasticsearch 基础 —— Query String
使用查询解析器来解析其内容的查询.下面是一个例子: GET /_search { "query": { "query_string" : { "def ...
- 【转载】Spring bean 中 constructor-arg属性
转载地址:https://blog.csdn.net/qq_27292113/article/details/78063696 方便以后查阅
- (转)yum的$releasever真是太反动了
Posted on 2009年 10月9日 by JulyClyde 来看这篇文章的人,大都应该同意<Unix编程艺术>中提到的那些观点吧.今天就给大家看一个反例:yum 的 $relea ...
- python列表解析和生成器表达式
列表解析作为动态创建列表的强大工具,值得学习. 列表解析技术之前的状况--函数式编程. lambda.filter(), map() enumerate, sorted, any, all, zip ...