LeetCode【206.反转链表】

题目描述

反转一个单链表。

示例:

  • 输入: 1->2->3->4->5->NULL

  • 输出:5->4->3->2->1->NULL

思路1

采用迭代的方式,改变各个结点的指针的方向。其重点在于首结点应该指向NULL

代码1

class Solution {
    public ListNode reverseList(ListNode head) {
   // 改变指针方向,Iterative
        ListNode dummpyRoot =new  ListNode(-1);
        ListNode node = head;
        while(node != null) {
            ListNode next = node.next;
            node.next = dummpyRoot.next;
            dummpyRoot.next = node;
            node = next;
        }
        return dummpyRoot.next;
      }
 }

复杂度分析:

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

思路2

采用递归的方式
(1)我们每次使用 h e a d . n e x t . n e x t = h e a d ; h e a d . n e x t = n u l l ; head.next.next = head; head.next = null; head.next.next=head;head.next=null;改变指针的方向。
(2)我们把迭代的方式改为递归(两个参数)的方式,每次递归相当于每次的迭代。

代码:

(1)

class Solution {
    public ListNode reverseList(ListNode head) {
 //Recursive
        if(head == null || head.next == null)
            return head;
        ListNode node = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return node;
        }
  }

(2)

class 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);
    }
  }

复杂度分析:

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

完整代码

package cn.zzuli.zcs8;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by 张超帅 on 2018/9/18.
 */
public class leetcode206 {


    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1,input.length() - 1);
        if(input.length() == 0) {
            return new int[0];
        }
        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for (int index = 0; index < parts.length; index ++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static ListNode stringToListNode(String intput) {
        int[] nodeValues = stringToIntegerArray(intput);
        ListNode dummyRoot = new ListNode(0);
        ListNode ptr = dummyRoot;
        for (int item : nodeValues) {
            ptr.next = new ListNode(item);
            ptr = ptr.next;
        }
        return dummyRoot.next;
    }
    public static String listNodeToString(ListNode head) {
        if(head == null) {
            return "[]";
        }
        String result = "";
        ListNode node = head;
        while (node != null) {
            result += Integer.toString(node.val) +", ";
            node = node.next;
        }
        return "[" + result.substring(0,result.length() - 2) +"]";
    }
    public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line ;
        while((line = in.readLine()) != null) {
            ListNode head = stringToListNode(line);
            ListNode ret = new Solution().reverseList(head);
            String out =listNodeToString(ret);
            System.out.println(out);
        }

    }
}
class ListNode {
    int val ;
    ListNode next;
    ListNode(int x) {val = x;}
}
class Solution {
    public ListNode reverseList(ListNode head) {
        /* 改变指针方向,Iterative
        ListNode dummpyRoot =new  ListNode(-1);
        ListNode node = head;
        while(node != null) {
            ListNode next = node.next;
            node.next = dummpyRoot.next;
            dummpyRoot.next = node;
            node = next;
        }
        return dummpyRoot.next;
        */
        //Recursive
        /*if(head == null || head.next == null)
            return head;
        ListNode node = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return node;
        */
        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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Rust 是一种现代的编程语言,特别适合处理内存安全和线程安全的代码。在 LeetCode 中,链表是经常出现的题目练习类型,Rust 语言也是一种非常适合处理链表的语言。接下来,本文将从 Rust 语言的特点、链表的定义和操作,以及 Rust 在 LeetCode链表题目的练习等几个方面进行介绍和讲解。 Rust 语言的特点: Rust 是一种现代化的高性能、系统级、功能强大的编程语言,旨在提高软件的可靠性和安全性。Rust 语言具有如下几个特点: 1. 内存安全性:Rust 语言支持内存安全性和原语级的并发,可以有效地预防内存泄漏,空悬指针以及数据竞争等问题,保证程序的稳定性和可靠性。 2. 高性能:Rust 语言采用了“零成本抽象化”的设计思想,具有 C/C++ 等传统高性能语言的速度和效率。 3. 静态类型检查:Rust 语言支持静态类型检查,可以在编译时检查类型错误,避免一些运行时错误。 链表的定义和操作: 链表是一种数据结构,由一个个节点组成,每个节点保存着数据,并指向下一个节点。链表的定义和操作如下: 1. 定义:链表是由节点组成的数据结构,每个节点包含一个数据元素和一个指向下一个节点的指针。 2. 操作:链表的常用操作包括插入、删除、查找等,其中,插入操作主要包括在链表首尾插入节点和在指定位置插入节点等,删除操作主要包括删除链表首尾节点和删除指定位置节点等,查找操作主要包括根据数据元素查找节点和根据指针查找节点等。 Rust 在 LeetCode链表题目的练习: 在 LeetCode 中,链表是常见的题目类型,而 Rust 语言也是一个非常适合练习链表题目的语言。在 Rust 中,我们可以定义结构体表示链表的节点,使用指针表示节点的指向关系,然后实现各种操作函数来处理链表操作。 例如,针对 LeetCode 中的链表题目,我们可以用 Rust 语言来编写解法,例如,链表,合并两个有序链表,删除链表中的重复元素等等,这样可以更好地熟悉 Rust 语言的使用和链表的操作,提高算法和编程能力。 总之,在 Rust 中处理链表是非常方便和高效的,而 LeetCode 中的练习也是一个非常好的机会,让我们更好地掌握 Rust 语言和链表数据结构的知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值