[LeetCode] Reverse Linked List(递归与非递归反转链表)

简介: Reverse a singly linked list.解题思路对于非递归实现,思路是依次将从第二个结点到最后一个结点的后继设为头结点,然后将该节点设为头结点(需记住将原头结点的后继设为空)。 对于递归实现,首先反转从第二个结点到最后一个结点的链表,然后再将头结点放到已反转链表的最后,函数返回新链表的头结点。非递归实现代码1[C++]//Run

Reverse a singly linked list.

解题思路

对于非递归实现,思路是依次将从第二个结点到最后一个结点的后继设为头结点,然后将该节点设为头结点(需记住将原头结点的后继设为空)。
对于递归实现,首先反转从第二个结点到最后一个结点的链表,然后再将头结点放到已反转链表的最后,函数返回新链表的头结点。

非递归实现代码1[C++]

//Runtime:10 ms
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (head == NULL) return NULL;
        ListNode *pre = head;
        ListNode *cur = head->next;
        while (cur != NULL)
        {
            pre->next = cur->next;
            cur->next = head;
            head = cur;
            cur = pre->next;
        }

        return head;
    }
};

非递归实现代码2[C++]

//Runtime:22 ms
class Solution{
public:
    ListNode* reverseList(ListNode* head){
        if (head == NULL) return NULL;
        ListNode *cur = head->next;
        head->next = NULL;
        while (cur != NULL)
        {
            ListNode *temp = cur->next;
            cur->next = head;
            head = cur;
            cur = temp;
        }

        return head;
    }
};

非递归实现代码3[Java]

//Runtime:276 ms
public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode cur = head.next;
        ListNode pre = head;
        while (cur != null){
            pre.next = cur.next;
            cur.next = head;
            head = cur;
            cur = pre.next;
        }

        return head;
    }
}

非递归实现代码4[Python]

#Runtime: 63 ms
# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    # @param {ListNode} head
    # @return {ListNode}
    def reverseList(self, head):
        if head == None:
            return head
        pre = head
        cur = head.next
        while cur != None:
            pre.next = cur.next
            cur.next = head
            head = cur
            cur = pre.next

        return head

s = Solution()
head = ListNode(0);
cur = head
for i in range(1, 10):
    node = ListNode(i)
    cur.next = node
    cur = node
head = s.reverseList(head);
while(head != None):
    print(head.val, end=' ')
    head = head.next

递归实现代码[C++]

//Runtime:10 ms
class Solution{
public:
    ListNode* reverseList(ListNode* head){
        //此处的条件不能写成if(head == NULL)
        if (head == NULL || head->next == NULL) return head;
        ListNode *newhead = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;

        return newhead;
    }
};

C++测试代码如下:

void printList(ListNode *head)
{
    while(head != NULL)
    {
        cout<<head->val<<" ";
        head = head->next;
    }
    cout<<endl;
}

void dropList(ListNode *head)
{
    if (head == NULL) return;
    ListNode *temp;
    while (head != NULL)
    {
        temp = head->next;
        delete head;
        head = temp;
    }
}

int main()
{
    ListNode *head = new ListNode(0);
    ListNode *cur = head;
    for (int i = 0; i < 10; i++)
    {
        ListNode *newnode = new ListNode(floor(rand()%100));
        cur->next = newnode;
        cur = newnode;
    }
    printList(head);
    Solution s;
    head = s.reverseList(head);
    printList(head);
    dropList(head);
}

Java测试代码如下:

public class AA {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ListNode head = new ListNode(0);
        ListNode cur = head;
        for(int i = 1; i < 10; i++){
            ListNode node = new ListNode(i);
            cur.next = node;
            cur = node;
        }

        Solution s = new Solution();
        head = s.reverseList(head);
        print(head);
    }

    static void print(ListNode head){
        while(head != null){
            System.out.println(head.val);
            head = head.next;
        }
    }
}
目录
相关文章
|
4月前
|
机器学习/深度学习 存储 算法
LeetCode 题目 95:从递归到动态规划实现 不同的二叉搜索树 II
LeetCode 题目 95:从递归到动态规划实现 不同的二叉搜索树 II
|
2月前
|
存储 Java
|
2月前
|
存储
LeetCode------递归(爬楼梯)
这篇文章通过LeetCode上的"爬楼梯"问题介绍了递归的基本概念和实现方法,包括递归公式的推导、基本递归实现、使用备忘录优化以避免重复计算,以及自底向上的迭代方法来提高效率。
LeetCode------递归(爬楼梯)
|
4月前
|
存储 算法 数据可视化
力扣156题最全解法:如何上下翻转二叉树(递归与迭代方法详解,附图解)
力扣156题最全解法:如何上下翻转二叉树(递归与迭代方法详解,附图解)
|
4月前
|
存储 算法 数据可视化
LeetCode 题目 96:从动态规划、递归到卡塔兰数实现不同的二叉搜索树
LeetCode 题目 96:从动态规划、递归到卡塔兰数实现不同的二叉搜索树
|
4月前
|
存储 算法 数据可视化
LeetCode 131题详解:高效分割回文串的递归与动态规划方法
LeetCode 131题详解:高效分割回文串的递归与动态规划方法
|
4月前
|
算法 数据可视化 数据挖掘
LeetCode题目104: 二叉树的最大深度(递归\迭代\层序遍历\尾递归优化\分治法实现 )
LeetCode题目104: 二叉树的最大深度(递归\迭代\层序遍历\尾递归优化\分治法实现 )
LeetCode题目104: 二叉树的最大深度(递归\迭代\层序遍历\尾递归优化\分治法实现 )
|
4月前
|
算法
【经典LeetCode算法题目专栏分类】【第11期】递归问题:字母大小写全排列、括号生成
【经典LeetCode算法题目专栏分类】【第11期】递归问题:字母大小写全排列、括号生成
|
4月前
|
存储 SQL 算法
LeetCode题目100:递归、迭代、dfs使用栈多种算法图解相同的树
LeetCode题目100:递归、迭代、dfs使用栈多种算法图解相同的树
|
4月前
|
存储 算法 数据可视化
LeetCode 题目 97:动态规划、递归到广度优先搜索BFS 实现交错字符串
LeetCode 题目 97:动态规划、递归到广度优先搜索BFS 实现交错字符串