查理
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 2023 年「[資訊科技產業專案設計](https://hackmd.io/@sysprog/info2023)」作業 2 #### 查理-Charlie #### :rabbit: : 我 #### :koala: : 面試官 [影片連結](https://youtu.be/47nKdqIsUuU) ## <font color="#7C8CD4">**206.Reverse Linked List**</font> ### **模擬面試過程** #### :koala: : 題目會給你一個指向一組Linked List的指標叫做 head,請你將這組Linked List進行反轉,並且回傳反轉後的Linked List。 #### :koala: : 請問你對題目有疑問嗎? #### :rabbit: : 有的,我想詢問關於輸入的Linked List 的邊界條件? #### :koala: : 輸入的Linked List 中其節點數的範圍是0~5000。 #### :rabbit: : 好的,爲了確認我對題目的理解,我想用一個例子説明。這道題讓輸入的Linked list 反轉,就是讓每個node 指向下一個node的指標反方向。 ![](https://hackmd.io/_uploads/Hk4BYKUZp.png) :::info 輸入: [1,3,5,7,9] 輸出: [9,7,5,3,1] ::: #### :rabbit: : 並且在結尾的node指向的下一個node 必須給定NULL,才能確定到了linked list 的最後。 #### :rabbit: :請問我對題目的理解正確嗎? #### :koala: : 正確,你可以開始寫你的程式了。 #### :rabbit: : 在寫程式之前,我想先講一下我的想法。 #### :rabbit: : 首先,我想到的是使用遞迴法來做這道題。我會將除了第一個節點的其他節點全部反轉,再接上原先的第一個節點,再接上NULL。 ``` class Solution { public: struct ListNode* reverseList(ListNode* head) { if(head == NULL || head->next == NULL){ return head; } struct ListNode* prev = NULL; struct ListNode* newhead = reverseList(head->next); head->next->next = head; head->next = prev; return newhead; } }; ``` #### :rabbit: : 使用這個方法的空間複雜度為O(n),時間複雜度也是O(n)。 #### :koala: : 除了用遞迴的方法之外,你覺得你還能用更好的方法解決這道題嗎? #### :rabbit: : 第二種方法我想到的是使用迭代法來做這道題。我會先宣告 3 個 node 指標:newhead、cur、next。cur 指向我們當前需要反向的 node,而 newhead 則是指向上一個反向完的 node,至於 next 則是先指向當前 node 在反向前所指向的下一個 node。 ``` class Solution { public: struct ListNode* reverseList(ListNode* head) { struct ListNode* newhead = NULL; struct ListNode* curr = head; while (curr != NULL){ struct ListNode* next = curr->next; curr->next = newhead; newhead = curr; curr = next; } return newhead; } }; ``` #### :rabbit: : 這個方法中我只宣告了 3 個指標,所以空間複雜度為O(1)。因為需要走訪每個 node 再改變他們指向的下一個的指標,所以時間複雜度為O(n)。 #### :koala: : 好的,謝謝你的作答。那我現在改變一下題目,我依然會給你一組Linked List,請你寫出可以判斷這組Linked List 是否回文的程式。請問你有任何問題嗎? #### :rabbit: : 有的,我想請問這組 Linked List 的邊界條件? #### :koala: : :Linked List 中的節點數範圍為1~10000。 #### :rabbit: : 好的,爲了確認我對題目的理解,我想用一個例子説明。 :::info 輸入: [1,3,5,3,1] 輸出: true 輸入: [1,3,5,7,9] 輸出: false ::: #### :rabbit: :請問我對題目的理解正確嗎? #### :koala: : 正確,你可以開始寫你的程式了。 #### :rabbit: : 在寫程式之前,我想先講一下我的想法。 #### :rabbit: : 首先,我會先將Linked List反轉,接著再逐一比較反轉後的Linked List 和原先的Linked List 是否一模一樣,如此便可以知道是否為回文。 ``` class Solution{ public: bool isPalindrome(ListNode* head){ if (head == NULL||head->next == NULL) { return head; } ListNode* newhead = NULL; ListNode* curr = head; while(curr!=NULL) { ListNode *temp = new ListNode(curr->val); temp ->next = newhead; newhead = temp; curr = curr->next; } while(head && newhead) { if (head->val != newhead->val) { return false; } head = head->next; newhead = newhead->next; } return true; } }; ``` #### :rabbit: :使用這個方法的空間複雜度為O(n),時間複雜度也是O(n)。 #### :koala: :好的,謝謝你。 --- ### **模擬面試檢討** ### <font color="#7C8CD4">**自評**</font> interviewer: 1. 口齒可以更加清晰。 interviewee: 1. 口齒可以更加清晰 2. 說話聲音再大一點 3. 一邊打程式一邊解說時,聲音會越來越小。 4. [3:12](https://www.youtube.com/watch?v=47nKdqIsUuU#t=3m12s) [4:10](https://www.youtube.com/watch?v=47nKdqIsUuU#t=4m10s)打錯字(正確: head、reverseList),應該多練習打程式的速度和正確率 --- ### <font color="#7C8CD4">**第一次作業的收穫**</font> interviewer: * 優點 1. 有開場白,可能會先介紹自己是哪一間公司...,先和interviewee稍微互動後才進入題目 2. 題目有做變形,延伸到一些真實情況上 interviewee: * 優點 1. 口齒清晰,聲音自然,語速穩定 2. 解釋程式時的思路清楚

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully