剑指Offer----链表中倒数第k个节点
来源:互联网 发布:北京节能环保中心 知乎 编辑:程序博客网 时间:2024/06/06 16:28
输入一个链表,输出该链表中倒数第k个结点。方法一:
/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode FindKthToTail(ListNode head,int k) { if(head==null||k<=0)return null; ListNode pAhead=head; ListNode pBhead=null; for(int i=0;i<k-1;i++){ if(pAhead.next!=null)pAhead=pAhead.next; else return null; } pBhead=head; while(pAhead.next!=null){ pBhead=pBhead.next; pAhead=pAhead.next; } return pBhead; }}
方法二:
public class Solution { public ListNode FindKthToTail(ListNode head,int k) { if (head == null) return null; ListNode node = head; int count = 0; while (node != null) { count++; node = node.next; } if (count < k) return null; ListNode p = head; for (int i = 0; i < count - k; i++) { p = p.next; } return p; }}
阅读全文
0 0
- 剑指Offer之链表中倒数第K个节点
- 来自《剑指offer》 链表中倒数第k个节点
- 【剑指offer系列】 链表中倒数第k个节点___15
- 剑指offer(16):链表中倒数第k个节点
- 剑指offer--15.链表中倒数第k个节点
- 剑指Offer :寻找链表中倒数第K个节点
- 剑指offer 链表中倒数第k个节点
- 剑指offer-链表中倒数第K个节点-php
- 剑指offer-链表中倒数第K个节点
- 剑指offer-14.链表中倒数第k个节点
- 剑指offer---链表中倒数第k个节点
- 剑指offer--链表中倒数第K个节点
- 剑指Offer----链表中倒数第k个节点
- 剑指offer——链表中倒数第k个节点
- 剑指Offer——链表中倒数第K个节点
- 剑指Offer--14.链表中倒数第k个节点
- 牛客网 剑指offer-链表中倒数第K个节点
- #剑指offer--(16)链表中倒数第k个节点
- 10进制转换为n进制
- 移动端h5开发基础
- ScrolView的Autolayout约束
- [leetcode]611. Valid Triangle Number
- Coverage algorithm for cleaning a room with limited area for each cleaning cycle
- 剑指Offer----链表中倒数第k个节点
- 1050: [HAOI2006]旅行comf
- 【maven】ITOO POM文件依赖关系
- iOS React Native 混合开发集成React Native
- GitHub+Hexo 搭建个人网站详细教程
- 【并查集】poj 2524 Ubiquitous Religions
- AVL树
- Oracle查询练习02
- SQL Server中,添加数据库到AlwaysOn高可用性组条件