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

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

全局:

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

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

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

2.3
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
EXAMPLE
Input: the node c from the linked list a->b->c->d->e
Result: nothing isreturned, but the new linked list looks like a- >b- >d->e

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


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


上一篇:【第三轮】6.23-6.29 CareerCup 2.2
下一篇:【第三轮】6.23-6.29 CareerCup 2.4
推荐
donnice 2014-6-29 03:54:46 | 只看该作者
全局:
【解题思路】
读出List长度,删除中间值后返回
删除:next.next
【时间复杂度】
O(1)
【空间复杂度】
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 LinkList{
        Node head = new Node();
        public void create(){
                Scanner sc = new Scanner(System.in);
                int x;
                for(x=sc.nextInt();x!=0;x=sc.nextInt())
                        insert(0,x);
        }
               
        public void insert(int i, Object t){
                Node p = head;
                int j = -1;
                while(p!=null && j<i-1){
                        p = p.getNext();
                        j++;
                }
                if(p == null || j>i-1)
                        System.out.print("cena");
                Node s = new Node(t);
                s.setNext(p.getNext());
                p.setNext(s);
        }

        public void RemoveMiddle(int i){
                Node p = head;
                int j = -1;
                while(p.getNext()!=null && j<i-1){
                        p = p.getNext();
                        j++;
                }
                p.setNext(p.getNext().getNext());
                Node t = head;
                while(t.getNext()!=null){
                        t = t.getNext();
                        System.out.print(t.getData()+" ");
                }
        }

        public int showLength(){
                Node p = head;
                int j = 0;
                while(p != null){
                        p = p.getNext();
                        j++;
                }
                return j;
        }

        public void display(){
                Node p = head.getNext();
                while(p!=null){
                        System.out.print(p.getData()+" ");
                        p = p.getNext();
                }
                System.out.println();
        }
}

public class Q2_2{
        public static void main(String[] args){
                LinkList L = new LinkList();
                Scanner sc = new Scanner(System.in);
                System.out.print("please insert the elements, 0 as an End:");
                L.create();
                L.display();
                int k = L.showLength();
                k = k/2;
                L.RemoveMiddle(k-1);
        }
}
回复

使用道具 举报

推荐
sanguine 2014-12-26 12:15:42 | 只看该作者
全局:
the problem is a little ambiguous, in the first time, what I understand is giving an input LinkedList and delete the middle node of the LinkedList. But actually, the true meaning is, just giving you a pointer to a node of a LinkedList, you should delete it and you can only access to that node.

In the solution below, I implement both the idea.

Question1
Problem: giving you a pointer to a node of a LinkedList, you should delete it and you can only access to that node

Solution
Before writing code, you should first consider the situation: what if the node to be deleted is the last node in the LinkedList? How to you handle it?

According to the description in the Book:

Note that this problem cannot be solved if the node to be deleted is the last node in the linked list. That’s ok—your interviewer wants you to point that out, and to discuss how to handle this case. You could, for example, consider marking the node as dummy.
In my solution, if the delete node is the last node, I just mark the value of the value of the node as MIN_VALUE and don’t print out.

Idea:
1) copy the next value to current
2) point current next to next’s next

Question2
Problem2: giving an input LinkedList and delete the middle node of the LinkedList

Solution
Before writing code, you should consider the situation below and ask the interviewer first!
1) what if the length of the input LinkedList is even like a->b->c->d?
2) in 1), if the requirement is to delete the c rather than b, what should you do?
3) If there is just two node in the input, what should we do?

Idea: two pointers, one runs slow, one runs fast(every time moves two node)

In the implementation below
1) I use the deleteNode() method in the Question1, if you don’t want to use the method, you should let the fast node go one step first, so that the slow pointer will at the previous node of the middle node, and using slow.next = slow.next.next to delete the middle node
2) When the length of the input LinkedList is even, for example, a->b->c->d, I just define the node b as the middle node. If define the C as the middle node, how do you change the code to implement it?

Time Complexity: O(n)
Space Complexity: O(n)

Code:
http://www.jyuan92.com/post-446
回复

使用道具 举报

全局:
本帖最后由 heycinderella 于 2014-6-26 02:55 编辑

【解题思路】
// Ask the interviewer if this means that the middlenode won't be the first or the last. If it is the first it doesn't matter. However if it is the last it can't be deleted. I assume the node that is to be deleted is not the last.

* Copy the value of each following node back one step, and put null after the 2nd to last node.
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/XiaoxiaoLi/f4ebb8143890558ac5ff

做完才发现书里答案那么简单,智商啊。。。其实就是把n下一个node的data copy过来,然后直接删掉n.next即可。。。那样时间空间都是O(1)了吧

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
readman 2014-6-23 01:12:34 | 只看该作者
全局:
【解题思路】
runner tech
【时间复杂度】
n
【空间复杂度】
1
【gist link】
https://gist.github.com/gaoyike/9088a0cc8d2d6af7694f
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
qianhuang 2014-6-23 10:21:14 | 只看该作者
全局:
【解题思路】
assign the value of its next to the node p, then delete the its next. consider following case:
   1. p is the middle node
   2. p is the last node (need to get more information from interviewer)
   3. p is the head
【时间复杂度】
O(1)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/qianhuang/4621219abc5b2e3153fb

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
chouclee 2014-6-23 11:26:26 | 只看该作者
全局:
【解题思路】将后一个node的所有信息复制给当前node,再删除后一个node。这么做的问题在于,实际当前node并没有被删除,如果还想要删除后一个node,程序会出错
【时间复杂度】O(1)
【空间复杂度】O(1)
【gist link】https://gist.github.com/chouclee/9b6beccbba1579ac0247

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
fang_wu 2014-6-23 19:27:29 | 只看该作者
全局:
【解题思路】将当前node的信息全都给后一个,同时删除前一个,但是如果是最后一个就不好考虑,那么可以设置为最小值,不打印出来
【时间复杂度】O(1)
【空间复杂度】O(1)
【gist link】https://gist.github.com/qiangusc/57bd879cda8daf6f0b2b

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
bearkino 2014-6-23 22:56:16 | 只看该作者
全局:
【解题思路】
如果只是传入node,对node进行操作,那么就是copy当前需要删除的node的下一个结点的数据到当前结点,然后当前结点连接下一个结点的next,跳过下一个结点,即删除了当前结点。

如果需要输出整个list,传入当前list, 和k值,for loop不添加需要删除的结点即可
【时间复杂度】
O(1)/ O(n)
【空间复杂度】
O(1)/ O(n)
【gist link】
https://gist.github.com/UncleGarden/f19d7c4a4a7666ef5144

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
grassgigi 2014-6-24 09:31:01 | 只看该作者
全局:
【解题思路】
Copy data of next node to the current node and delete next node.
For the last node, we should consider it specially. since Java always pass value to function(even for reference), so there is no way to set the last node to null. (now I miss my C# and ref..)

【时间复杂度】
O(1)

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/chrislukkk/37c7a2b140795abff900

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
林微熙 2014-6-24 15:24:15 | 只看该作者
全局:
【解题思路】
把下一个复制到这个,删下一个
【时间复杂度】
1
【空间复杂度】
1
【gist link】https://gist.github.com/hilda8519/00ffa4ba7b468b6f6e85

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
wilbert 2014-6-25 03:25:43 | 只看该作者
全局:
【解题思路】
copy the value of next node to the node given (to be deleted), and delete the next node. The following conditions are NOT considered.
(1) given node at the beginning
(2) given node at the end
【时间复杂度】
O(1)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/iwilbert/e32064c46369092cba64

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

🔗
锦木千束 2014-6-25 23:04:31 | 只看该作者
全局:
【解题思路】空node/最后一个node不可能删除,返回;  
该node复制下一个node的全部信息,这样实际上是删除下一个node,但用下一个node的值替换了想要删除的node的值
【时间复杂度】O(1)
【空间复杂度】O(1)
【gist link】https://gist.github.com/weazord/054b3fd99671699db121

评分

参与人数 1大米 +7 收起 理由
wrj5518 + 7

查看全部评分

回复

使用道具 举报

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

本版积分规则

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