搜狐一题面试题 链表的排序
来源:互联网 发布:mac 日历 广告 编辑:程序博客网 时间:2024/05/01 11:10
今天一同学去参加搜狐的笔试,说是遇到了一题链表的排序题
一般采用merge sort ,只是链表的排序和数组的排序不太一样,毕竟你得把链表跑一边才知道他是怎样的
#include <stdio.h>#include <stdlib.h>struct node{ int data; struct node* next;};struct node* Sort(node*a,node*b);struct node* MergeSort(node* head){ node* a = NULL; node* b = NULL; if(head == NULL||head->next == NULL)return head;struct node* fast; struct node* slow; slow = head; fast = head->next; while(fast != NULL) { fast = fast->next; if( fast != NULL ) { slow = slow->next; fast = fast->next; } } a = head; b = slow->next; slow->next = NULL; a = MergeSort(a); b = MergeSort(b);return Sort(a,b);}struct node* Sort(node*a,node*b){struct node* result;if(a==NULL)return b;if(b==NULL)return a;if(a->data <= b->data) { result = a; result->next = Sort(a->next, b); } else { result = b; result->next = Sort(a, b->next); } return result;} void printList(struct node* node) //打印链表{ while( node != NULL ) { printf("%d ", node->data); node = node->next; } }自己写了一段很蠢的测试代码
int main() { struct node* head = NULL; struct node* a,*b;struct node* ans = NULL; a =b= (struct node*)malloc(sizeof(struct node));a->data = 2;head = a;b=a;a = (struct node*)malloc(sizeof(struct node));a->data = 3;b ->next = a;b = a;a = (struct node*)malloc(sizeof(struct node));a->data = 15;b ->next = a;b = a;a = (struct node*)malloc(sizeof(struct node));a->data = 4;b ->next = a;b = a;b->next = NULL; ans = MergeSort(head); printf("Sorted Linked List is: \n"); printList(ans);//链表输出 return 0; }
0 0
- 搜狐一题面试题 链表的排序
- 搜狐和软信通面试题
- 搜狐面试题 2011.07
- 搜狐笔试、面试题
- 面试题15:合并两个排序的链表
- 【面试题十七】合并两个排序的链表
- 面试题17 合并两个排序的链表
- 面试题17:合并两个排序的链表
- 面试题17:合并两个排序的链表
- 面试题17合并两个排序的链表
- 面试题17:合并两个排序的链表
- 面试题17:合并两个排序的链表
- 《剑指Offer》面试题:合并两个排序的链表
- 面试题19:合并两个排序的链表(offer)
- 面试题17 合并两个排序的链表
- 剑指offer面试题 合并两个排序的链表
- 面试题17:合并两个排序的链表
- 面试题17:合并两个排序的链表
- win7下修改mysql5.0 root 密码
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- python中关于时间的处理
- android数据存储(一)之Shared Preferences及注意事项
- android网络编程——使用Android中的网络连接
- 搜狐一题面试题 链表的排序
- OCP 1Z0 051 149
- 温家宝在河北省兴隆县六道河中学作地理讲座
- nginx缓存
- C++课程设计(图书管理系统)
- android开发Day1
- 从网站细节入手提高易用性
- 枚举
- 【Flume NG用户指南】(1)设置