1. 程式人生 > >LeetCode(連結串列)【206.反轉連結串列】

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(1)O(1)

思路2

採用遞迴的方式 (1)我們每次使用he

ad.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)

完整程式碼

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