[LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list.
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;
}
};
类似题目:
All LeetCode Questions List 题目汇总
最新文章
- Centos7下用命令同步标准时间
- 帝国cms内容批量替换
- ubuntu14.04计划任务无法执行
- why does turn off button means hibernate on my win8
- PHP OAuth2 Server库
- accelerated C++ 中查找url(学习笔记)
- c# datatable list 相互转换
- Java读取WEB-INF目录下的properties配置文件
- Javascript中null值,特别注意的两点
- 物联网操作系统 - Contiki
- bzoj3574[Hnoi2014]抄卡组
- DHTML【2】--HTML
- CSS Hack代码与浏览兼容总结
- C++随机数的使用方法
- webpack前端工程化构建工具的使用
- JS异常
- eval函数的特点和作用
- MySQL 基础 DDL和DML
- IUSER 匿名帐户密码获取
- RecyclerView的滚动事件OnScrollListener研究
热门文章
- 使用Arduino开发板实现与MPU6050陀螺仪传感器连接的方法
- selenium中的等待方法及区别
- npm run dev 报错 iview TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
- 多项式的各类计算(多项式的逆/开根/对数/exp/带余除法/多点求值)
- tornado处理跨域问题
- LeetCode 979. Distribute Coins in Binary Tree
- PostgreSQL 一些比较好用的字符串函数
- jmeter使用教程
- 好的想法只是OKR的开始--创业者谨记
- ROM