查看: 3894| 回复: 33
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.23-6.29 CareerCup 2.2

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 wrj5518 于 2014-6-23 09:32 编辑

2.2 Implement an algorithm to find the kth to last element of a singly linked list.

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。


上一篇:【第三轮】6.23-6.29 CareerCup 2.1
下一篇:【第三轮】6.23-6.29 CareerCup 2.3
推荐
bitcpf 2014-6-25 00:22:59 | 只看该作者
全局:
【解题思路】2 pointers, the first one move k steps then move the pointers together till the the first one touch the end of the list
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/bitcpf/b66f30e9790e08189bb7
回复

使用道具 举报

推荐
donnice 2014-6-28 05:48:48 | 只看该作者
全局:
【解题思路】用两个node,p先走k格,q接上。p走完时,输出剩下的p
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】无gist,代码在此
import java.util.*;
class Node{
        private Node next;
        private Object data;
        public Node(){
                this(null,null);
        }
        public Node(Object data){
                this(data,null);
        }
        public Node(Object data, Node next){
                this.data = data;
                this.next = next;
        }
        public Object getData(){
                return data;
        }
        public void setData(Object data){
                this.data = data;
        }
        public Node getNext(){
                return next;
        }
        public void setNext(Node next){
                this.next = next;
        }
}

class LinkedList{
        Node head = new Node();
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        public void create(){
                for(int i = 0; i<a; i++)
                        insert(0,i);
        }

        public void insert(int i, Object x){
                Node p = head;
                int j = 0;
                while(p!= null && j<i){
                        p = p.getNext();
                        j++;
                }
                if(p == null || j>i)
                        System.out.print("cena");
                Node s = new Node(x);
                s.setNext(p.getNext());
                p.setNext(s);
        }

        public void display(){
                Node p = head.getNext();
                while(p!=null){
                        System.out.print(p.getData()+" ");
                        p = p.getNext();
                }
        System.out.println();
        }
       
        public void displaykth(int k){
                Node q = head.getNext();
                int j = 0;
                while(j<k){
                        q = q.getNext();
                        j++;
                }
                Node s = head.getNext();
                while(q!=null){
                        s = s.getNext();
                        q = q.getNext();
                }
                while(s!=null){
                        System.out.print(s.getData()+" ");
                        s = s.getNext();
                }
        }
}

public class Q2{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                System.out.print("please insert the length of the list:");
                LinkedList L = new LinkedList();
                L.create();
                L.display();
                System.out.print("please insert the int k:");
                int k = sc.nextInt();
                L.displaykth(k);
        }
}
                 

点评

为什么不用gist= =  发表于 2014-6-28 08:29
回复

使用道具 举报

🔗
chouclee 2014-6-23 11:19:53 | 只看该作者
全局:
本帖最后由 chouclee 于 2014-6-23 15:16 编辑

【解题思路】先traverse linkedlist,获得linkedlist的长度N,在从头开始寻找第N-k+1个元素
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/chouclee/03cf7577a2ec37c6b311
回复

使用道具 举报

🔗
bearkino 2014-6-23 15:13:08 | 只看该作者
全局:
本帖最后由 bearkino 于 2014-6-23 15:21 编辑

【解题思路】
首先p1先走k步,然后p1,p2同时走,当p2走到尾端的时候,p1刚好在kth这个位置,距离尾端是k
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/UncleGarden/ab5491edaa4c81b08b3d
回复

使用道具 举报

🔗
readman 2014-6-23 16:41:39 | 只看该作者
全局:
【解题思路】
首先p1先走k步,然后p1,p2同时走,当p2走到尾端的时候,p1刚好在kth这个位置,距离尾端是k
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/gaoyike/9eef63abd6756e1f13f8
回复

使用道具 举报

🔗
fang_wu 2014-6-23 19:24:26 | 只看该作者
全局:
【解题思路】
两个指针
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/qiangusc/d83a59d1efe2c95bf424
回复

使用道具 举报

🔗
grassgigi 2014-6-24 09:03:02 | 只看该作者
全局:
【解题思路】
Runner tech

【时间复杂度】
O(n)

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/chrislukkk/a8edbb93ccd07acbdf0b
回复

使用道具 举报

🔗
林微熙 2014-6-24 14:13:21 | 只看该作者
全局:
【解题思路】
recursive
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/hilda8519/c3d7f3fe212b926024bf
回复

使用道具 举报

🔗
锦木千束 2014-6-24 22:44:50 | 只看该作者
全局:
本帖最后由 锦木千束 于 2014-6-24 22:46 编辑

【解题思路】先获取总长度,再get倒数第k个
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/weazord/e37a37e895e9e406eafe

点评

我错了。。。 用的自己定义的链表,创建的时候就有length 就想怎么可能这么简单  发表于 2014-6-24 22:46
回复

使用道具 举报

🔗
wilbert 2014-6-24 23:17:15 | 只看该作者
全局:
【解题思路】
two pointers p1 and p2, move p2 for k steps first, then move them together until p1 reaches the end of the list, p2 is the answer
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/iwilbert/700a22f8aca7b52544f5
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
length of the list is smaller than k
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表