`

Reverse a linked list

 
阅读更多

原创转载请注明出处:http://agilestyle.iteye.com/blog/2360694

 

Recursive Idea

Reverse(Head -> Remaining List)

=>

Reverse(Remaining List) -> Head

 

Example:

Reverse(1->2->3->4->5)

=>

Reverse(2->3->4->5) -> 1

 

Demo

package org.fool.java.collections;

public class ReverseLinkedListTest {
    public static void main(String[] args) {
        List l = new List(1);
        l.next = new List(2);
        l.next.next = new List(3);
        l.next.next.next = new List(4);
        l.next.next.next.next = new List(5);

        System.out.println("Original List: " + l.toString());

        l = reverse(l);

        System.out.println("Reversed List: " + l.toString());
    }

    public static List reverse(List l) {
        // firstly check if l is empty or only has one element then return
        if (l == null || l.next == null) {
            return l;
        }

        // otherwise, use recursive method to process
        List remainingReverse = reverse(l.next);

        // step 1: need to update the tail of remaining reverse as head l
        l.next.next = l;    // this (l.next) is the key to get the tail in constant time

        // set l.next to NULL after that! Otherwise it's causing cycles in list
        l.next = null;

        // step 2: return the reverse list
        return remainingReverse;
    }
}

class List {
    int value;
    List next;

    public List(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        List current = this;
        String output = "";

        while (current != null) {
            output += current.value + " -> ";
            current = current.next; // increment the pointer index current
        }

        return output + "NULL";
    }
}

Note: 


Console Output


 

Reference

https://www.youtube.com/watch?v=j5m6rRszzEQ&list=PLlhDxqlV_-vkak9feCSrnjlrnzzzcopSG 

 

  • 大小: 29.1 KB
  • 大小: 11.8 KB
分享到:
评论

相关推荐

    c语言-leetcode题解之0092-reverse-linked-list-ii.zip

    在文件“c语言-leetcode题解之0092-reverse-linked-list-ii.zip”中,我们可以通过解压缩得到文件“0092_reverse_linked_list_ii”。这个文件很可能是C语言编写的代码文件,包含了实现“反转链表II”题目的算法逻辑...

    陈越、何钦铭-数据结构作业6:Reversing Linked List链表翻转

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→...

    linked-list-reverse-output.rar_Reverse Linked List

    在计算机科学领域,数据结构是基础且至关重要的概念,它为高效地存储和处理数据提供了方法。本话题聚焦于一种常见的线性数据结构——单链表,并探讨如何对其进行逆序输出。逆序输出单链表涉及到对链表节点顺序的反转...

    js-leetcode题解之92-reverse-linked-list-ii.js

    JavaScript解题之92号问题:反转链表II 在JavaScript中,反转链表是一个...最终,通过上述步骤,我们可以得到一个完整的js-leetcode题解之92-reverse-linked-list-ii.js的代码实现,成功地对链表的一部分进行了反转。

    java-leetcode题解之206-Reverse-Linked-List

    Java LeetCode题解之206-Reverse-Linked-List是指在Java编程语言中对LeetCode上的第206号题目“反转链表”进行解答的过程。这是一个经典的算法与数据结构问题,通常作为算法入门的练习题,用于练习对链表这种数据...

    leetcode不会-Leetcode-Java:Leetcode-Java

    leetcode 不会 Leetcode Solutions in Java Linked List Linked List ...a linked list, ...a ...快慢指针法,块指针从head.next开始,慢指针从head开始,快指针每次移动两格,...reverseList(ListNode head) 三个指针,依次往后

    基本数据结构及算法习题参

    7. **逆转线性链表(Reverse a Linked List)**:逆转链表的算法通过三个指针p、q、r实现。首先设置p为链表头,q为p的下一个节点,p的next指针设为null,然后在循环中不断更新p、q、r,使得q指向p的下一个节点,p的...

    C++-leetcode题解之206-Reverse-Linked-List.cpp

    C++中反转单链表是程序员在使用leetcode进行数据结构和算法训练时经常会遇到的一个经典问题。题目的主要任务是改变链表中节点的指向,使得链表从头到尾的顺序变为从尾到头。在C++中,链表节点通常通过结构体或类来...

    python-leetcode题解之206-Reverse-Linked-List.py

    在编程领域,特别是在使用Python语言解决算法问题时,LeetCode平台提供了一个非常宝贵的资源。今天我们要探讨的是如何使用Python来解决LeetCode上的一个经典题目——206号问题,即反转一个单链表。...

    fuxuemingzhu#Leetcode-Solution-All#92. Reverse Linked List II 反转

    进行一次遍历,把第m到n个元素进行翻转,即依次插入到第m个节点的头部。这个题还是有意思的。建议后面再多做几遍。Python代码如下:self.next = No

    前端大厂最新面试题-linked-list.docx

    2. 反转链表(Reverse Linked List) 反转链表是将链表的方向反转,即将链表的头节点变为尾节点,尾节点变为头节点。这种操作可以用于解决一些特殊的问题,例如,反转链表以满足某些算法的要求。 知识点:反转链表...

    danlianbiao.rar_Linked list_链表

    `Reverse`方法则需要两个指针,一个记录当前节点,另一个记录前一个节点,依次交换节点的`Next`指针,直到遍历完整个链表。 项目的实现细节可能包含异常处理,如确保插入和删除操作时不会超出链表范围,或者在空...

    一个示例代码,演示了如何在PTA中实现逆序数据建立链表的过程

    build_reverse_linked_list 函数: 接收一个列表 data,将其逆序构建成链表。 首先使用 data.reverse() 反转输入的数据。 创建一个虚拟头节点 dummy,然后遍历反转后的数据,依次创建新的节点并连接成链表。 返回...

    数据结构英文教学课件:chapter4 Array and Linked list exercise.ppt

    本课件“Chapter4 Array and Linked list Exercise”聚焦于两种基础且重要的数据结构:数组(Array)和链表(Linked list),这些都是编程中不可或缺的基础知识。 1. **数组**: 数组是一种线性数据结构,它包含...

    LeetCode2 Add Two Numbers

    You are given two non-empty linked lists ... Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. java AC版本

    什么是reverse常见的reverse有哪些

    链表反转(Linked List Reverse) 链表是一种常用的数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。链表反转是指将链表中节点的指针方向逆转。例如,将链表`1->2->3->4`反转为`4->3->2->...

    Reverse-A-Linked-List:动画蛇游戏,可视化反转链表的算法

    关于 这个项目包含了一个蛇游戏,但是有点扭曲。 在这里,我已经实现了反向链接算法,并且可以类似地用于可视化相同的算法。 可用脚本 在项目目录中,可以运行: npm start 在开发模式下运行该应用程序。...

    python 教程 leetcode 代码模板-.Linked-List-Two-Pointers-List.md

    **题目描述**:给定一个单链表L的头结点head,将其重新排列为A->B->A->B->...的形式。 **解决方案**:首先找到链表的中点(使用快慢指针),然后反转后半部分链表,最后交替合并两段链表。 ```python def reorder...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    92-反转链表II:reverse-linked-listt-ii 141-环形链表:linked-list-cycle 142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的...

    The-reverse-of-sqlist.rar_就地逆置

    这里的`createLinkedList`和`printList`是辅助函数,分别用于生成链表和打印链表,它们可以根据实际需求进行编写。 总的来说,C++中单链表的就地逆置是一个常见的算法问题,它涉及到链表的基本操作和指针的巧妙使用...

Global site tag (gtag.js) - Google Analytics