LeetCode #206 Reverse Linked List 反轉鏈結串列

英文原文如下

Given the head of a singly linked list, reverse the list, and return the reversed list.

中文翻譯

給予一個單向的鏈結串列的頭節點,反轉整個鏈結串列並回傳。

範例及題目限制

進階 : 一個鏈結串列可以使用迴圈或是遞迴的方式來反轉,你可以將兩種都解出來嗎?


解題思路

鏈結串列、反轉,這兩個關鍵字注定了第一種寫法。

必定就是List跟迴圈,將全部資料蒐集起來,用內建的反轉,再重建鏈結串列回去!!

C# 解決方案

方案1 – 感覺超簡單但超級不好的方式

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if (head == null) 
        {
            return null;
        }

        List<int>list = new List<int>();
        
        while(head!=null)
        {
            list.Add(head.val);
            head = head.next;
        }

        list.Reverse();

        ListNode resNode = new ListNode();
        
        resNode.val = list[0];
        ListNode lastNode = resNode;
        ListNode nowNode = null;
        
        for(int i = 1; i<list.Count;i++)
        {
            nowNode = new ListNode();
            nowNode.val = list[i];
            lastNode.next = nowNode;
            
            lastNode = nowNode;
        }

        return resNode;
    }
}

使用了一個額外的 List 來儲存 ➡ 額外空間

方案2

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if (head == null) 
        {
            return null;
        }

        ListNode prev = null;
        ListNode curr = head;

        while (curr != null) 
        {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }

        return prev;
    }
}

直接用一個While跑到底,一邊反轉一邊進行下一步

Java解決方案

方案1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) 
        {
            return null;
        }

        ListNode prev = null;
        ListNode curr = head;

        while (curr != null) 
        {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }

        return prev;
    }
}

Python3 解決方案

方案1

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head:
            return None

        prev = None
        curr = head

        while curr:
            next_node = curr.next
            curr.next = prev
            prev = curr
            curr = next_node

        return prev

JavaScript 解決方案

方案1

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    if (!head) 
    {
        return null;
    }

    let prev = null;
    let curr = head;

    while (curr) 
    {
        let nextNode = curr.next;
        curr.next = prev;
        prev = curr;
        curr = nextNode;
    }

    return prev;
};

結論

鏈結串列重要的是理解他的概念,很多時候我也會忘記自己現在在哪裡哈哈

都在靠著console印出來才知道

🧡如果這篇文章有幫上你的一點點忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Reverse Linked List – LeetCode

一些隨機文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *