leetcode题解-19. Remove Nth Node From End of List
来源:互联网 发布:seo牛人 编辑:程序博客网 时间:2024/05/22 04:54
题意:给一个链表和数字n,移除倒数第n个数,返回head。
例子:
给定链表 1->2->3->4->5, 并且n = 2.
返回链表 1->2->3->5.
分析:题目比较简单,就是数据结构中链表的常规删除操作而已。需要注意的是,有可能删除的是头结点,因此需要声明头节点的前置节点dummyNode。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head == null || head.next == null) return null; int len = 0; ListNode dummyNode = new ListNode(0); dummyNode.next = head; ListNode fastNode = head; ListNode slowNode = dummyNode; while(fastNode != null){ len++; fastNode = fastNode.next; } for(int i = 0; i < len - n; i++){ slowNode = slowNode.next; } slowNode.next = slowNode.next.next; return dummyNode.next; }}
阅读全文
0 0
- Leetcode题解 19. Remove Nth Node From End of List
- leetcode题解-19. Remove Nth Node From End of List
- LeetCode题解:Remove Nth Node from End of List
- leetcode 题解 || Remove Nth Node From End of List 问题
- LeetCode题解:Remove Nth Node From End of List
- 19. Remove Nth Node From End of List题解
- 19. Remove Nth Node From End of List leetcode list
- leetcode题解-328. Odd Even Linked List && 19. Remove Nth Node From End of List
- [LeetCode]19.Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- LeetCode --- 19. Remove Nth Node From End of List
- LeetCode 19.Remove Nth Node From End of List
- [Leetcode] 19. Remove Nth Node From End of List
- [leetcode] 19.Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- leetcode 19. Remove Nth Node From End of List
- capstone训练营2017-12-11
- 360木马查杀后mysql数据不能启动
- MIT6.00 1x 第一讲-计算机科学简介-笔记
- 算法导论 第十章 基本数据结构 练习10.1-2
- codeforces 896C
- leetcode题解-19. Remove Nth Node From End of List
- WinPE VirtIO云主机版 支持west263 阿里云aliyun 送精简win2k3镜像
- eclipse版本和jdk对应关系
- CC NOV14 Chef and Churu 分块+BIT维护单点.
- Python基础知识-网络编程
- 若元组包含可变对象 对象改变后 元组不变
- 语义网基础教程 (第一版)6 应用
- [POJ](3723)Conscription ---- 最小生成树(Kruskal)
- 在eclipse中集成maven集成的基本步骤