C/C++ 微软面试题 -链表
来源:互联网 发布:国家燃烧知乎 编辑:程序博客网 时间:2024/05/01 11:12
链表的结点结构
struct Node{int data ;Node *next ;};typedef struct Node Node ;
(1)已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)
Node * ReverseList(Node *head) //链表逆序{if ( head == NULL || head->next == NULL )return head;Node *p1 = head ;Node *p2 = p1->next ;Node *p3 = p2->next ;p1->next = NULL ;while ( p3 != NULL ){p2->next = p1 ;p1 = p2 ;p2 = p3 ;p3 = p3->next ;}p2->next = p1 ;head = p2 ;return head ;}
(2)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
Node * Merge(Node *head1 , Node *head2){if ( head1 == NULL)return head2 ;if ( head2 == NULL)return head1 ;Node *head = NULL ;Node *p1 = NULL;Node *p2 = NULL;if ( head1->data < head2->data ){head = head1 ;p1 = head1->next;p2 = head2 ;}else{head = head2 ;p2 = head2->next ;p1 = head1 ;}Node *pcurrent = head ;while ( p1 != NULL && p2 != NULL){if ( p1->data <= p2->data ){pcurrent->next = p1 ;pcurrent = p1 ;p1 = p1->next ;}else{pcurrent->next = p2 ;pcurrent = p2 ;p2 = p2->next ;}}if ( p1 != NULL )pcurrent->next = p1 ;if ( p2 != NULL )pcurrent->next = p2 ;return head ;}
(3)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 (Autodesk)
Node * MergeRecursive(Node *head1 , Node *head2){if ( head1 == NULL )return head2 ;if ( head2 == NULL)return head1 ;Node *head = NULL ;if ( head1->data < head2->data ){head = head1 ;head->next = MergeRecursive(head1->next,head2);}else{head = head2 ;head->next = MergeRecursive(head1,head2->next);}return head ;
8 0
- C/C++ 微软面试题 -链表
- C/C++ 微软面试题剖析
- C语言面试题二——微软篇
- C/C++微软亚洲技术中心面试题及答案
- C语言面试题大汇总之微软亚洲技术中心面试题
- C语言面试题大汇总之微软亚洲技术中心面试题
- C语言面试题大汇总之微软亚洲技术中心面试题
- C语言面试题大汇总之微软亚洲技术中心面试题
- C语言面试题大汇总之微软亚洲技术中心面试题
- C语言面试题大汇总之微软亚洲技术中心面试题
- C语言面试题大汇总之微软亚洲技术中心面试题
- [C/C++]各种面试题
- .C/C++/嵌入式面试题
- [C/C++]各种面试题
- [C/C++]各种面试题
- C++/C面试题集
- C语言面试题
- c/c++面试题
- OGG-01028 unexpected LOB record or record fragment
- 四舍五入
- IOS 开源库 asyncSocket 的使用心得
- 黑马程序员-----Date和Calendar
- Java synchronized详解
- C/C++ 微软面试题 -链表
- IOS 生成pem证书方法
- 下一代的APACHE HADOOP MAPREDUCE : YARN
- pthread_create如何传递多个参数
- error: no matching function for call to
- 我们玩新浪微博
- 字典树——Message Flood
- Spring注解@Component、@Repository、@Service、@Controller区别
- 受限玻尔兹曼机(RBM)学习笔记(七)RBM 训练算法