Cracking coding interview(2.2)返回单向链表的倒数第n个节点
来源:互联网 发布:网络弱电施工标准 编辑:程序博客网 时间:2024/06/08 07:20
2.2 Implement an algorithm to find the nth to last element of a singly linked list.
1.先计算单向链表长度,再查找倒数第n个元素
2.使用2个Node节点指针pre,p,使pre指向head,p指向第n个节点,之后同步向后同步移动到p到达单向链表的最后一个节点为止,此时pre的值即指向单向链表倒数第n节点。(此方法一开始没想到,很妙!)
class Node{int data;Node next;public Node(int data, Node next){this.data = data;this.next = next;}}public class Solution{//time complexity:O(n), generally thinkingpublic static Node getLastN1(Node head, int n){if(n <= 0)return null;//counter length of singly linked listint len=0;Node p = head;for(;p != null;p = p.next)len++;if(len < n)return null;elselen -= n;//find last n nodep = head;for(;p != null;p = p.next)if(len == 0)return p;elselen--;return null;}//time complexity:O(n), optimum solutionpublic static Node getLastN2(Node head, int n){if(n <= 0)return null;Node p = head;//step forward n-1 stepsfor(;n != 1 && p != null;p = p.next){n--;}if(p == null)return null;//pre and p synchronization forwardNode pre = head;for(;p.next != null;p = p.next, pre = pre.next);return pre;}private static void printNode(Node head){Node p = head;for(;p != null;p = p.next)System.out.print(p.data+" ");System.out.println();}public static void main(String[] args){System.out.println("args.length="+args.length);Node head = new Node(1, null);head.next = new Node(2, null);head.next.next = new Node(3, null);head.next.next.next = new Node(4, null);head.next.next.next.next = new Node(5, null);head.next.next.next.next.next = new Node(6, null);head.next.next.next.next.next.next = new Node(7, null);Solution.printNode(head);Node p = Solution.getLastN1(head, 0);System.out.println("n=0:"+ (p == null ? p : p.data));p = Solution.getLastN1(head, 3);System.out.println("n=3:"+ (p == null ? p : p.data));p = Solution.getLastN1(head, 1);System.out.println("n=1:"+ (p == null ? p : p.data));p = Solution.getLastN1(head, 17);System.out.println("n=17:"+ (p == null ? p : p.data));Solution.printNode(head);p = Solution.getLastN2(head, 0);System.out.println("n=0:"+ (p == null ? p : p.data));p = Solution.getLastN2(head, 3);System.out.println("n=3:"+ (p == null ? p : p.data));p = Solution.getLastN2(head, 1);System.out.println("n=1:"+ (p == null ? p : p.data));p = Solution.getLastN2(head, 17);System.out.println("n=17:"+ (p == null ? p : p.data));}}
0 0
- Cracking coding interview(2.2)返回单向链表的倒数第n个节点
- 删除单向链表倒数第n个节点
- 返回链表中的倒数第N个节点
- 寻找单向链表的倒数第k个节点
- 单向链表的倒数第k个节点
- Cracking coding interview(2.4)单向链表相加
- 《Cracking the Coding Interview程序员面试金典》----链表中倒数第k个结点
- 《Cracking the Coding Interview程序员面试金典》----链表中倒数第k个结点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 链表倒数第n个节点
- 向Linux上游社区提交补丁
- 合格机构或几个环节改革
- HDU 1072:Nightmare(BFS)
- HDU 1052 Tian Ji -- The Horse Racing (贪心)
- C++程序设计(1)之初步知识
- Cracking coding interview(2.2)返回单向链表的倒数第n个节点
- 好地方上的花费不会的发生的
- 双系统重装win7/Xp后如何恢复ubuntu引导
- 做官经验
- PAT1005. Spell It Right (20)
- iosLearing高级开发~Runtime(二)
- Memcache架构新思考
- git学习笔记
- IT忍者神龟之jdk全集