Reverse Linked List(反转单向链表)

来源:https://leetcode.com/problems/reverse-linked-list

Reverse a singly linked list.

递归方法:递归调用直到最后一个节点再开始反转,注意保存反转后的头结点返回

Java

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 class Solution {
10     public ListNode reverseList(ListNode head) {
11         if(head == null || head.next == null) {
12             return head;
13         }
14         ListNode newHead = reverseList(head.next);
15         head.next.next = head;
16         head.next = null;
17         return newHead;
18     }
19 }

Python

 1 # -*- coding:utf-8 -*-
 2 # 递归
 3 # class ListNode:
 4 #     def __init__(self, x):
 5 #         self.val = x
 6 #         self.next = None
 7 class Solution:
 8     # 返回ListNode
 9     def ReverseList(self, pHead):
10         # write code here
11         if pHead == None or pHead.next == None:
12             return pHead
13         tmp = self.ReverseList(pHead.next)
14         pHead.next.next = pHead
15         pHead.next = None
16         return tmp

迭代方法:两个指针从头开始依次反转,注意保存下一次反转的节点指针

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode pre = head, cur = head.next, next = null;
        while(cur != null) {
            next = cur.next;
            cur.next = pre;
            if(pre == head) {
                pre.next = null;
            }
            pre = cur;
            cur = next;
        }
        return pre;
    }
}

Python

# -*- coding:utf-8 -*-
# 迭代
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        if pHead == None or pHead.next == None:
            return pHead
        pre_node, cur_node = pHead, pHead.next
        while pre_node and cur_node:
            tmp_node = cur_node.next
            cur_node.next = pre_node
            pre_node = cur_node
            cur_node = tmp_node
        pHead.next = None
        return pre_node
时间: 2024-08-17 02:28:28

Reverse Linked List(反转单向链表)的相关文章

leetCode 206. Reverse Linked List 反转链表

206. Reverse Linked List Reverse a singly linked list. 反转一个链表. 思路: 采用头插法,将原来链表重新插一次返回即可. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode(int x) : val(x), next(NULL) {}  * };

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

(每日算法)LeetCode --- Reverse Linked List II(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n sati

[算法]反转单向链表和双向链表

题目: 分别实现反转单向链表和双向链表的函数. 要求: 如果链表长度为N,时间复杂度为O(N),额外空间复杂度要求为O(1). 程序: 反转单向链表: public class Node{ public Node(int data){ this.value=data; } public int value; public Node next; } public static Node reverseList(Node node){ Node pre=null; Node next=null; w

反转单向链表(JAVA)

在微博看到,有人说8个应届毕业生没有人写出o(1)空间复杂度,o(n)时间复杂度的反转单向链表. (不是我自己想的) public void reverseList(ListNode head) { ListNode newHead = null; while(head != null) { ListNode next = head.next; head.next = newHead; newHead = head; head = next; } return newHead; } 自己也想了很

Leetcode:Reverse Linked List II 单链表区间范围内逆置

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

C语言反转单向链表的代码

学习过程中中,把内容过程中常用的内容片段做个珍藏,下边内容段是关于C语言反转单向链表的内容,应该能对大伙有较大用处. #include "stdafx.h" enum{N = 3};class Node{public:int var;Node(int i):pNext(NULL), var(i){}}; {if(pHead->pNext->pNext != NULL)helper(pHead->pNext, reverseHead);elsereverseHead =

[LeetCode]77. Reverse Linked List反转链表

Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Subscribe to see which companies asked this question 解法1:一个最简单的办法就是借助栈的后进先出功能,先扫描一遍链表保存每个节点的值,然后再

[LeetCode] 206. Reverse Linked List ☆(反转链表)

Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3->2->1->NULL 进阶: 你可以迭代或递归地反转链表.你能否用两种方法解决这道题? 解析 设置三个节点pre.cur.next (1)每次查看cur节点是否为NULL,如果是,则结束循环,获得结果 (2)如果cur节点不是为NULL,则先设置临时变量next为cur的下一个节点 (3)让cur

leetcode_92题——Reverse Linked List II(链表操作)

Reverse Linked List II Total Accepted: 40420 Total Submissions: 154762My Submissions Question Solution Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, retur