206. Reverse Linked List【easy】

Reverse a singly linked list.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

解法一:

 class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * pre = NULL; while (head != NULL) {
ListNode * next = head->next;
head->next = pre;
pre = head;
head = next;
} return pre;
}
};

解法二:

 class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !(head -> next)) return head;
ListNode* node = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return node;
}
};

参考了@jianchao.li.fighter 的代码

The basic idea of this recursive solution is to reverse all the following nodes after head. Then we need to set head to be the final node in the reversed list. We simply set its next node in the original list (head -> next) to point to it and sets its next to be NULL.

解法三:

 public ListNode reverseList(ListNode head) {
/* recursive solution */
return reverseListInt(head, null);
} private ListNode reverseListInt(ListNode head, ListNode newHead) {
if (head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}

参考了@braydenCN 的代码

206. Reverse Linked List【easy】的更多相关文章

  1. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  2. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

  3. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  4. 234. Palindrome Linked List【Easy】【判断链表是否回文】

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  5. 876. Middle of the Linked List【Easy】【单链表中点】

    Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...

  6. 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  8. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  9. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

随机推荐

  1. [BZOJ 1804] Flood

    Link: BZOJ 1804 传送门 Solution: 不容易啊,第一道完全自己A掉的IOI题目..... 算法思想其实很简单: 模拟缩减的过程即可 将每条边转为2条有向边,每次找到最左边的边,沿 ...

  2. 【块状树】bzoj3731 Gty的超级妹子树

    带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...

  3. 触摸事件onTouchListener

    1.效果图: (1)MainAcivity.java package com.example.app3; import android.content.DialogInterface; import ...

  4. Android中选项卡功能的实现

    Android中选项卡功能的实现 Android中使用TabHost和TabWidget来实现选项卡功能.TabHost必须是布局的根节点,它包含两个子节点: TabWidget,显示选项卡: Fra ...

  5. jQuery--样式

    Jquery(一)——样式篇1.$(document).ready 的作用是等页面的文档(document)中的节点都加载完毕后,再执行后续的代码, 因为我们在执行代码的时候,可能会依赖页面的某一个元 ...

  6. java实现发送邮件功能

    项目中实现发送邮件功能,先书写一个小Demo,记录如下: POM.XML中导入依赖 <!-- start java 提供的支持邮件发送相关业务的类 --> <dependency&g ...

  7. postprocessing stack v2

    用了v2和unity2017.3.0f有兼容性问题 在assetbundle的情况下 CopyStd这个shader打不进去 在assetbundle的menafest里面有列但是shader.fin ...

  8. Vue组件进阶知识总结

    上一篇我们重点介绍了组件的创建.注册和使用,熟练这几个步骤将有助于深入组件的开发.另外,在子组件中定义props,可以让父组件的数据传递下来,这就好比子组件告诉父组件:“嘿,老哥,我开通了一个驿站,你 ...

  9. 【Hadoop】HDFS源码解读

    1.open流程 2.get DFS流程: 3.获取block信息流程

  10. 常见BUG问题汇总[待更新]

    1.字符串数据库长度问题,特别是与java接口对接的过程中要注意 2.存储数据库之前所有的数据都需要在存储前进行验证