LeetCode【206.反转链表】

本文介绍两种实现单链表反转的方法:迭代法和递归法。迭代法通过改变节点指针方向实现,而递归法则利用递归特性完成反转过程。文章提供了完整的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

反转一个单链表。

示例:

  • 输入: 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)我们每次使用head.next.next=head;head.next=null;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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值