剑指Offer14 逆序链表
/*************************************************************************
> File Name: 14_ReverseListNode.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月30日 星期二 15时47分32秒
************************************************************************/ #include <stdio.h>
#include <malloc.h> // 链表结构体
struct ListNode
{
int val;
struct ListNode* next;
}; // 构造链表
ListNode* createList()
{
struct ListNode* head;
struct ListNode* p;
struct ListNode* q;
head = p = (ListNode*)malloc(sizeof(ListNode));
head->val = ;
for (int i = ; i <= ; ++i)
{
q = (ListNode*)malloc(sizeof(ListNode));
q->val = i;
p->next = q;
p = q;
}
p->next = NULL;
return head;
} // 顺序输出链表
void PrintList(ListNode* head)
{
if (head == NULL)
return;
ListNode* temp = head;
printf("PrintList:\n");
while (temp != NULL)
{
printf("%d ", temp->val);
temp = temp->next;
}
printf("\n");
} // 逆序链表
ListNode* ReverseList(ListNode* head)
{
if (head==NULL || head->next==NULL)
return head;
ListNode* fast = head->next;
ListNode* slow = head;
slow->next = NULL;
ListNode* temp; while (fast->next != NULL)
{
temp = fast->next;
fast->next = slow;
slow = fast;
fast = temp;
}
fast->next = slow;
return fast;
} int main()
{
ListNode* test = createList();
PrintList(test);
test = ReverseList(test);
PrintList(test);
return ;
}
剑指Offer14 逆序链表的更多相关文章
- 剑指Offer03 逆序输出链表&链表逆序
多写了个逆序链表 /************************************************************************* > File Name: ...
- 剑指Offer-14:输入一个链表,输出该链表中倒数第k个结点。
题目描述: 输入一个链表,输出该链表中倒数第k个结点.例如有一个链表有六个节点1,2,3,4,5,6.则它的倒数第二个节点为5 节点定义如下: public class ListNode { int ...
- 《剑指offer》复杂链表的复制
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- 《剑指offer》 反转链表
本题来自<剑指offer> 反转链表 题目: 输入一个链表,反转链表后,输出新链表的表头. 思路: 需要三个变量,来保存当前节点的,前面节点和反转后的节点. C++ Code: /* st ...
- 剑指Offer:删除链表的节点【18】
剑指Offer:删除链表的节点[18] 题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3-& ...
- 剑指Offer:反转链表【24】
剑指Offer:反转链表[24] 题目描述 输入一个链表,反转链表后,输出新链表的表头. 解题分析 这道题我才发现我是属于那种真的笨,图都画出来了流程写不出来.看了别人的代码,总觉得自己差一步. 这也 ...
- [剑指 Offer 18. 删除链表的节点]
[剑指 Offer 18. 删除链表的节点] 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点. 返回删除后的链表的头节点. 注意:此题对比原题有改动 示例 1: 输入: head ...
- 剑指 Offer 35. 复杂链表的复制
剑指 Offer 35. 复杂链表的复制 Offer_35 题目详情 方法一 可以使用一个HashMap来存储旧结点和新结点的映射. 这种方法需要遍历链表两遍,因为需要首先知道映射关系才能求出next ...
- 剑指 Offer 24. 反转链表
剑指 Offer 24. 反转链表 Offer 24 题目描述: 常规解法 本题的解法很常规,没有其他特别的坑,只需要将链表反转即可. package com.walegarrett.offer; / ...
随机推荐
- 网页上的JS call Unity3d里的function——SendMessage
注意: sendmessage只可以从网页发信息到unity游戏里,但是没有返回值 只可以发布三种类型的data,不可以其他复杂的强类型 发信息的时不会做编译检测 SendMessage Workfl ...
- c# abstract抽象类与继承类子类的构造函数_base
http://blog.itpub.net/9240380/viewspace-718054/ http://blog.163.com/cloud_thegreat/blog/static/10367 ...
- iPhone中国移动收不到彩信,联通不用设置都可以,具体设置方法:
打开“设置”. 打开“通用”. 打开“蜂窝移动网络”. 打开“蜂窝数据移动网络”. 在“蜂窝移动数据”一栏中的“APN”处填入“cmnet”. 在“彩信”一栏中的“APN”处填入“cmnet”,“MM ...
- 异步文件上传组件 Uploader
Uploader是非常强大的异步文件上传组件,支持ajax.iframe.flash三套方案,实现浏览器的全兼容,调用非常简单,内置多套主题支持 和常用插件,比如验证.图片预览.进度条等,广泛应用于淘 ...
- TAxThread - Inter thread message based communication - Delphi
http://www.cybletter.com/index.php?id=3 http://www.cybletter.com/index.php?id=30 Source Code http:// ...
- js页面文字选中后分享到新浪微博实现
demo您可以狠狠地点击这里:js文字选中分享到新浪微博demo 方法与代码 选中即分享的功能看上去比较高级,其实实现是相当简单的.其中的会让人头大,一般人也不感兴趣的原理这里就直接跳过.这个js文字 ...
- Gmail POP3设置
好几个同事在问我怎样使用ThunderBird和OE收取IT CHT的邮箱,因为IT CHT就是用Gmail的功能,因此收发邮件是跟Gmail一样,下面是Gmail的POP&SMTP的设置方法 ...
- Python int与string之间的转化
string-->int 1.10进制string转化为int int('12') 2.16进制string转化为int int('12', 16) int-->string 1.int转 ...
- Codeforces Gym 100531D Digits 暴力
Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...
- [Angular2 Form] Use RxJS Streams with Angular 2 Forms
Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...