Reverse a singly linked list.

Hint:

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

反向链表,分别用递归和迭代方式实现。

递归Iteration:

新建一个node(value=任意值, next = None), 用一个变量 next 记录head.next,head.next指向新node.next,新 node.next 指向head,next 进入下一个循环,重复操作,直到结束。

迭代Recursion:

假设有链表1 -> 2 -> 3 -> 4 -> 5要进行反转,第一层先把1 -> 2 -> 3 -> 4 -> 5和None传入递归函数(head, newhead),先记录head的next(2 -> 3 -> 4 -> 5),head(1)指向newhead(None)变成(1->None),然后把(2 -> 3 -> 4 -> 5,  1 -> None)递归传入下一层,记录head的next(3 -> 4 -> 5),head(2)指向newhead(1->None)变成(2 -> 1->None),然后把(3 -> 4 -> 5,  2 -> 1 -> None)递归传入下一层,直到最后head=None时返回newhead,此时 newhead = 5 -> 4 -> 3 -> 2 -> 1 -> None

Java: Iterative solution

public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while (head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}

Java: Recursive solution

public ListNode reverseList(ListNode head) {
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);
}  

Python: Iteration

def reverseList(self, head):
prev = None
while head:
curr = head
head = head.next
curr.next = prev
prev = curr
return prev

Python: Iteration

def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
cur, prev = head, None
while cur:
cur.next, prev, cur = prev, cur, cur.next
return prev

Python: Iteration

# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None def __repr__(self):
if self:
return "{} -> {}".format(self.val, repr(self.next)) class Solution:
def reverseList(self, head):
dummy = ListNode(0)
while head:
next = head.next
head.next = dummy.next
dummy.next = head
head = next
return dummy.next

Python: Recrusion

class Solution(object):
def reverseList(self, head, prev=None):
if not head:
return prev curr, head.next = head.next, prev
return self.reverseList(curr, head)  

Python: Recursion

class Solution:
def reverseList(self, head):
return self.doReverse(head, None)
def doReverse(self, head, newHead):
if head is None:
return newHead
next = head.next
head.next = newHead
return self.doReverse(next, head)

Python: Recursion, wo

class Solution(object):
def reverseList(self, head):
dummy = ListNode(0)
cur = self.recur(head, dummy)
return cur.next def recur(self, head1, head2):
if not head1:
return head2 next_node = head1.next
head1.next = head2.next
head2.next = head1
return self.recur(next_node, head2)  

C++: Iteration

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
auto dummy = ListNode{0}; while (head) {
auto tmp = head->next;
head->next = dummy.next;
dummy.next = head;
head = tmp;
} return dummy.next;
}
};

C++: Recursion

class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: The new head of reversed linked list.
*/
ListNode *reverse(ListNode *head) {
if (!head || !head->next) return head;
ListNode *t = head;
head = reverse(t->next);
t->next->next = t;
t->next = NULL;
return head;
}
};

   

类似题目:

[LeetCode] Reverse Linked List II 反向链表II

All LeetCode Questions List 题目汇总

最新文章

  1. Centos7下用命令同步标准时间
  2. 帝国cms内容批量替换
  3. ubuntu14.04计划任务无法执行
  4. why does turn off button means hibernate on my win8
  5. PHP OAuth2 Server库
  6. accelerated C++ 中查找url(学习笔记)
  7. c# datatable list 相互转换
  8. Java读取WEB-INF目录下的properties配置文件
  9. Javascript中null值,特别注意的两点
  10. 物联网操作系统 - Contiki
  11. bzoj3574[Hnoi2014]抄卡组
  12. DHTML【2】--HTML
  13. CSS Hack代码与浏览兼容总结
  14. C++随机数的使用方法
  15. webpack前端工程化构建工具的使用
  16. JS异常
  17. eval函数的特点和作用
  18. MySQL 基础 DDL和DML
  19. IUSER 匿名帐户密码获取
  20. RecyclerView的滚动事件OnScrollListener研究

热门文章

  1. 使用Arduino开发板实现与MPU6050陀螺仪传感器连接的方法
  2. selenium中的等待方法及区别
  3. npm run dev 报错 iview TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
  4. 多项式的各类计算(多项式的逆/开根/对数/exp/带余除法/多点求值)
  5. tornado处理跨域问题
  6. LeetCode 979. Distribute Coins in Binary Tree
  7. PostgreSQL 一些比较好用的字符串函数
  8. jmeter使用教程
  9. 好的想法只是OKR的开始--创业者谨记
  10. ROM