C++面试题

当前位置: 面试问题网 > C++面试题 > 链表面试题-一个链表的结点结构

链表面试题-一个链表的结点结构

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 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 ;
   }

【链表面试题-一个链表的结点结构】相关文章

1. 链表面试题-一个链表的结点结构

2. 写出程序把一个链表中的接点顺序倒排

3. 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?

4. 编写一子程序,将一链表倒序,即使链表表尾变表头,表头变表尾

5. Oracle面试题:如何把一个表从一个schema到另一个schema

6. 编程实现去掉XML的重复结点

7. 写一个在SQL Server创建表的SQL语句

8. Can a struct inherit from another struct? (结构体能继承结构体吗)

9. 简述Linux文件系统通过i节点把文件的逻辑结构和物理结构转换的工作过程

10. 对工伤认定的结果不服怎么办?

本文来源:https://www.mianshiwenti.com/a13105.html

点击展开全部

《链表面试题-一个链表的结点结构》

将本文的Word文档下载到电脑,方便收藏和打印

推荐程度:

进入下载页面

﹝链表面试题-一个链表的结点结构﹞相关内容

「链表面试题-一个链表的结点结构」相关专题

其它栏目

也许您还喜欢