版权声明: https://blog.csdn.net/qq_38386316/article/details/82766591
题目描述
反转一个单链表。
示例:
-
输入: 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;
}
}
复杂度分析:
时间复杂度:
空间复杂度:
思路2
采用递归的方式
(1)我们每次使用
改变指针的方向。
(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);
}
}
复杂度分析:
时间复杂度:
空间复杂度:
完整代码
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);
}
}