剑指offer 16题 【代码的鲁棒性】反转链表
来源:互联网 发布:诈骗 知乎 编辑:程序博客网 时间:2024/05/19 11:44
题目描述
输入一个链表,反转链表。
牛客地址:http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
/** * 16.反转链表 * */public class Title16 {/** * 书中代码 */public ListNode ReverseList(ListNode head) {ListNode before = null,cur = head, next = null,retNode = null;while(cur != null){next = cur.next;if(next == null)retNode = cur; cur.next = before; before = cur; cur = next;}return retNode; }/** * 自己写的代码 */public ListNode ReverseList(ListNode head) {if(head == null)return null;ListNode before = null,cur = head, next = cur.next;while(next != null){cur.next = before;before = cur;cur = next;next = cur.next;}cur.next = before; // 这里注意return cur; }public static void main(String[] args) {ListNode a = new ListNode(1);ListNode b = new ListNode(2);ListNode c = new ListNode(3);ListNode d = new ListNode(4);a.next =b;b.next =c;c.next = d;d.next = null;ListNode head = new Title16().ReverseList(a);while(head != null){System.out.print(head.val + " ");head = head.next;}}}
以前写的代码
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* ReverseList(ListNode* pHead) { ListNode *p,*pre,*last; p=last=pHead; pre=NULL; while(p != NULL){ last=p->next; p->next=pre; pre=p; p=last; } return pre; }};
0 0
- 剑指offer 16题 【代码的鲁棒性】反转链表
- 剑指offer:(16)代码的鲁棒性:反转链表
- 剑指offer 3.4 代码的鲁棒性2- 反转链表
- 【剑指offer】3.4代码的鲁棒性——面试题16:反转链表
- 《剑指offer》刷题笔记(代码的鲁棒性):反转链表
- 16链表的反转|剑指offer
- 【剑指offer】题16:反转链表
- 剑指offer算法题之单链表的反转--面试题16:反转链表
- 剑指offer面试题16 反转链表(递归和非递归,主要还是看代码的鲁棒性)
- 剑指offer 16- 反转链表
- 剑指Offer 16 反转链表
- 剑指offer-16:反转链表
- 剑指offer面试16 反转链表
- 剑指offer 16 反转链表
- 剑指offer:反转链表
- 剑指offer--反转链表
- 剑指offer--反转链表
- 剑指Offer-反转链表
- Java千百问_02基本使用(013)_linux系统如何管理环境变量
- Java 开发环境配置
- 关于angularjs中多个ng-app该如何启用的问题
- seajs
- 外观模式
- 剑指offer 16题 【代码的鲁棒性】反转链表
- 怎样用jmp指令改变CS,IP
- python中的items方法
- hdu 4844 公路巡逻 dp(略难)
- gson的@Expose注解和@SerializedName注解
- 一个简单的登录页面
- hbase 12.1 regionserver 宕机报错记录——1
- NavigationController返回按钮颜色和文字
- picasso_强大的Android图片下载缓存库