i****1 发帖数: 445 | 1 明天微软的电面,题目已经提前发给我了,不知道是什么意思,难道微软让我在电话里
讲代码?。
merge two already sorted list.
list 节点是
class Node{
public:
int data;
Node * next;
};
这个题目我觉得不是很难。
Node * merge (Node * head1, Node * head2)
{
//让我填写这个函数。
}
请教各位大牛,这个题目会不会有什么陷阱? |
M***t 发帖数: 1636 | 2 一点bug都不能出
【在 i****1 的大作中提到】 : 明天微软的电面,题目已经提前发给我了,不知道是什么意思,难道微软让我在电话里 : 讲代码?。 : merge two already sorted list. : list 节点是 : class Node{ : public: : int data; : Node * next; : }; : 这个题目我觉得不是很难。
|
t*********h 发帖数: 941 | 3 微软越来越没节操了阿
【在 i****1 的大作中提到】 : 明天微软的电面,题目已经提前发给我了,不知道是什么意思,难道微软让我在电话里 : 讲代码?。 : merge two already sorted list. : list 节点是 : class Node{ : public: : int data; : Node * next; : }; : 这个题目我觉得不是很难。
|
s***y 发帖数: 203 | 4 这个估计是coding test,面试和这个没关系,最后和面试一起evaluation |
i****1 发帖数: 445 | 5 Node * Merge(Node * head1, Node * head2)
{
Node * head3, *tail;
head3 = tail = new Node;
while(head1 && head2)
{
if (head1 -> data < head2 -> data)
{
tail -> next = head1;
tail = head1;
head1 = head1 -> next;
}
else
{
tail -> next = head2;
tail = head2;
head2 = head2 -> next;
}
}
if (head1)
{
tail -> next = head1;
}
else
{
tail -> next = head2;
}
tail = head3;
head3 = head3 -> next;
delete tail;
tail = NULL;
return head3;
}
这个怎么样?
【在 M***t 的大作中提到】 : 一点bug都不能出
|
b******7 发帖数: 92 | 6 没有必要,应该也不允许你申请内存
head3 = tail = new Node
另外,判断指针最好要用if(head == NULL)
【在 i****1 的大作中提到】 : Node * Merge(Node * head1, Node * head2) : { : Node * head3, *tail; : head3 = tail = new Node; : while(head1 && head2) : { : if (head1 -> data < head2 -> data) : { : tail -> next = head1; : tail = head1;
|
i****1 发帖数: 445 | 7 嗯。判断指针确实用head == NULL好。
请问为何不允许分配内存?
不分配一个辅助接点的话,while循环里面每次都要判断tail是不是空 (tail为空表示
第一次进入while),这样难道不会影响程序效率?
而且后面的两个if里也要判断tail是不是空。
【在 b******7 的大作中提到】 : 没有必要,应该也不允许你申请内存 : head3 = tail = new Node : 另外,判断指针最好要用if(head == NULL)
|
c***f 发帖数: 40 | 8 楼主是new grad 吗? 怎么拿到电面的呢?
前几天发邮件问微软的hr, 回答说 这个学年的 new grad都招得差不多了, 看来是电
面都不会给我一个了。。。
【在 i****1 的大作中提到】 : 明天微软的电面,题目已经提前发给我了,不知道是什么意思,难道微软让我在电话里 : 讲代码?。 : merge two already sorted list. : list 节点是 : class Node{ : public: : int data; : Node * next; : }; : 这个题目我觉得不是很难。
|
|
c*****a 发帖数: 808 | 9 把recursive和iterative都写出来吧 |
j**7 发帖数: 143 | 10 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
if(head1==null &&head2==null)
{
return null;
}
if(head1==null)
return head2;
if(head2==null)
return head1;
ListNode head=null;
ListNode result=null;
ListNode current1=head1;
ListNode current2=head2;
while (current1 != null && current2 != null) {
if (current1 . val < current2 .val) {
if (head == null) {
head = current1;
result = current1;
}
else
{
head . next = current1;
head = head . next;
}
current1 =current1.next;
} else {
if (head == null) {
head = current2;
result = current2;
}
else
{
head . next = current2;
head = head . next;
}
current2 = current2 . next;
}
}
if (current1 == null) {
head . next = current2;
} else {
head . next = current1;
}
return result;
}
} |