iT邦幫忙

2025 iThome 鐵人賽

DAY 21
0
生成式 AI

Chatting with ChatGPT——一天學習一題Leetcode系列 第 21

LeetCode 206. Reverse Linked List

  • 分享至 

  • xImage
  •  

題目大意:給你一個單向鏈結串列的頭節點 head,把整個串列反轉並回傳新的頭節點。
範例:1 -> 2 -> 3 -> null 反轉後變成 3 -> 2 -> 1 -> null

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }
}

上一篇
倒數10天!LeetCode 58. Length of Last Word
系列文
Chatting with ChatGPT——一天學習一題Leetcode21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言