LeetCode算法題-Reverse Linked List(Java實現)

這是悅樂書的第192次更新,第195篇原創 <br/>node

01 看題和準備

今天介紹的是LeetCode算法題中Easy級別的第51題(順位題號是206)。反轉單鏈表。例如:算法

輸入:1-> 2-> 3-> 4-> 5 輸出:5-> 4-> 3-> 2-> 1數組

本次解題使用的開發工具是eclipse,jdk使用的版本是1.8,環境是win7 64位系統,使用Java語言編寫和測試。 <br/>數據結構

02 第一種解法

先利用遞歸函數,進入到最後一個節點的位置,此時須要反轉指針所指向的引用方向,好比原來是n(k)-->n(k+1),如今須要反轉過來變成n(k+1)-->n(k),此時須要n(k).next.next = n(k),將n(k+1)的下一個節點指向n(k),同時須要將原來n(k)節點的下一個節點指向null,即n(k).next = null。若是不指向null,會造成環,形成死循環。eclipse

public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    ListNode p = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return p;
}

<br/>函數

03 第二種解法

在遍歷列表時,將當前節點的下一個指針更改成指向其上一個元素。因爲節點沒有引用其前一個節點,所以必須事先存儲其前一個元素。在更改引用以前,還須要另外一個指針來存儲下一個節點。最後返回新的鏈表。工具

public ListNode reverseList2(ListNode head) {
    ListNode result = null;
    ListNode current = head;
    while (current != null) {
        ListNode pre = current.next;
        current.next = result;
        result = current;
        current = pre;
    }
    return result;
}

<br/>開發工具

04 第三種解法

利用HashMap,依次遍歷head節點,將下一個節點做爲key、當前節點做爲value存入其中,直到其最後一個節點。新建立一個節點指向head的最後一個節點,而後開始從map中取出key所對應的value做爲新節點的下一個節點。測試

public ListNode reverseList3(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    HashMap<ListNode, ListNode> nodeMap = new HashMap<>();
    ListNode curr = head;
    int leng = 0;
    while (curr.next != null) {
        nodeMap.put(curr.next, curr);
        ++leng;
        curr = curr.next;
    }
    ListNode newHead = curr;
    for (int i = 0; i < leng; i++) {
        curr.next = nodeMap.get(curr);
        curr = curr.next;
    }
    curr.next = null;
    return newHead;
}

<br/>spa

05 第四種解法

使用棧,藉助其先進後出的特色,先將head的每個節點入棧,而後新建一個節點res,每次出棧的節點,獲取其節點值val,而後建立新的節點做爲res的下一個節點。

public ListNode reverseList4(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    Stack<ListNode> stack = new Stack<>();
    while (head != null) {
        stack.push(head);
        head = head.next;
    }
    ListNode res = new ListNode(-1);
    ListNode temp = res;
    while (!stack.isEmpty()) {
        temp.next = new ListNode(stack.pop().val);
        temp = temp.next;
    }
    return res.next;
}

<br/>

06 第五種解法

此解法與第四種解法思路相似,只不過是將棧換成了數組,而後新建node節點,以數組最後一位元素做爲節點值,而後開始循環處理每一個新的節點。

public ListNode reverseList5(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    while (head != null) {
        list.add(head.val);
        head = head.next;
    }
    ListNode node = new ListNode(list.get(list.size()-1));
    ListNode last = node;
    for (int i=list.size()-2; i >= 0; i--) {
        ListNode temp = new ListNode(list.get(i));
        last.next = temp;
        last = last.next;
    }
    return node;
}

<br/>

07 小結

算法專題目前已連續日更超過一個月,算法題文章51+篇,公衆號對話框回覆【數據結構與算法】、【算法】、【數據結構】中的任一關鍵詞,獲取系列文章合集。

以上就是所有內容,若是你們有什麼好的解法思路、建議或者其餘問題,能夠下方留言交流,點贊、留言、轉發就是對我最大的回報和支持!

相關文章
相關標籤/搜索