/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
if(!head||!head->next)
return head;
struct ListNode *cur=head;
struct ListNode *tail=head;
while(tail->next)
tail=tail->next;
while(cur!=tail)
{
head=cur->next;
cur->next=tail->next;
tail->next=cur;
cur=head;
}
return head;
}

有一个问题,leetcode给出的链表测试数据应该是不包含头结点,所以写代码的时候需要注意一下。

还有另外一种解法,就是把所有指针的方向反过来,代码如下。

struct ListNode* reverseList(struct ListNode* head) {
if(head&&head->next)
{
struct ListNode* pre=NULL;
struct ListNode* cur=head;
struct ListNode* pnext=cur->next; while(pnext)
{
cur->next=pre;
pre=cur;
cur=pnext;
pnext=cur->next;
}
cur->next=pre;
head=cur;
}
return head;
}

最新文章

  1. 3.Windows Server 2012 R2数据库部署
  2. NK3C系统中ID的汉语名称
  3. kali 忘记登录密码后重置的方法
  4. odoo报表条码无法显示解决[转]
  5. jquery 获取radio选中的值
  6. iframe标签用法详解(属性、透明、自适应高度)
  7. PHP中用下划线开头的变量含义
  8. JAXB - Hello World
  9. JSF学习五Ajax
  10. C#6.0语法糖
  11. vs2010编译live555源码
  12. 验证API
  13. 学好php可以做的事情真多!
  14. Node.js系列文章:编写自己的命令行界面程序(CLI)
  15. codeforces#1132 F. Clear the String(神奇的区间dp)
  16. ES6 - Map
  17. 笨鸟先飞之ASP.NET MVC系列之过滤器(04认证过滤器)
  18. 大数据学习笔记02-HDFS-常用命令
  19. 【转】vue.js表单校验详解
  20. 每日英语:Chinese Show Global Real-Estate Appetite

热门文章

  1. Cordova+Asp.net Mvc+GIS
  2. android XML格式颜色
  3. 可以使用QT给龙芯开发软件
  4. web本地存储-IndexedDB
  5. denoising autoencoder
  6. UICollectionView基础学习
  7. hdu 5115 Dire Wolf(区间dp)
  8. 【Problem】Count and Say
  9. mongodb 2.6.6 在window10下的安装
  10. IT人员----怎么把电脑窗口设置成淡绿色