Robin Chien
    • 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
    ###### tags: `資訊產業導論` # info2021-homework1 > 貢獻者: 李馬克 Mark ## 測驗一、 206. Reverse Linked List ![](https://i.imgur.com/K1EC6dO.png) 已知不存在環狀結構,且為單向鍊結串列,因此只須將 linked list 其內節點指向反轉即可 對應程式碼如下 ```java= //定義 Node 結構 class Node() { int value; Node nextNode; Node(int val) { this.value = value; } } class Solution() { // 疊代版本 private Node reverseNodes(Node head){ Node curr = head; Node prev = null; while(curr != null) { Node temp = curr.nextNode; curr.nextNode = prev; prev = curr; curr = temp; } return prev; } // 遞迴版本 private Node reverseNodeRecursive(Node head) { if(head == null || head.nextNode == null) { return head } Node newNode = reverseNodeRecursive(head.nextNode) head.nextNode.nextNode = head; head.nextNode = null; return newNode; } private printLinkedList(Node head) { while(head != null) { System.out.print(head.value + " "); head = head.nextNode; } System.out.print("\n"); } public static void main(String[] args) { Node head = new Node(1); head.nextNode = new Node(2); head.nextNode.nextNode = new Node(3); head.nextNode.nextNode.nextNode = new Node(4); head.nextNode.nextNode.nextNode.nextNode = new Node(5); printLinkedList(reverseNodes(head)); printLinkedList(reverseNodeRecursive(head)); } } ``` 輸出: ```java= 5 4 3 2 1 5 4 3 2 1 ``` ## 測驗二、 141. Linked List Cycle Given head, the head of a linked list, determine if the linked list has a cycle in it. ![](https://i.imgur.com/uawyMt9.png) 對應程式碼如下 ```java= //定義 Node 結構 class Node() { int value; Node nextNode; } class Solution() { // 用 HashSet 檢查是否有重複的 Node private boolean hasCycle(Node head) { HashSet<Node> set = new HashSet(); while(head != null) { if(set.contains(head)){ return true; } else { set.add(head); head = head.nextNode; } } return false; } // 利用兩個 Node 的前進速度不同,檢查是否會相撞 private boolean hasCycleWithTwoNodes(Node head) { if (head == null) { return false; } Node slow = head; Node fast = head.nextNode; while(slow != fast) { if (fast == null || fast.nextNode == null) { return false; } slow = slow.nextNode; fast = fast.nextNode.nextNode; } return true; } public static void main(String[] args) { Node head = new Node(3); head.nextNode = new Node(2); head.nextNode.nextNode = new Node(0); head.nextNode.nextNode.nextNode = new Node(-4); head.nextNode.nextNode.nextNode.nextNode = head.nextNode; System.out.println(hasCycle(head)); System.out.println(hasCycleWithTwoNodes(head)); } } ``` 輸出: ```java= true true ``` :::info 討論項目 - 是否能在 O(1) Memory Space 下解決此問題 ::: ## 測驗三、 142. Linked List Cycle II Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. ![](https://i.imgur.com/SjF2giv.png) ```java= class Node() { int value; Node nextNode; } class Solution() { // 用 HashSet 找出產生 cycle 的 Node private Node beginCycleNode(Node head) { HashSet<Node> set = new HashSet(); while(head != null) { if(set.contains(head)) { return head; } else { set.add(head); head = head.nextNode; } } return null; } // 找出"開始"、"產生 cycke"、"碰撞"此三個 Node 的距離關係 private Node beginCycleNodeTwo(Node head) { Node fast = head; Node slow = head; while(slow != null && fast != null) { slow = slow.nextNode; fast = fast.nextNode.nextNode; if(slow == fast) { break; } } if (fast == null || fast.nextNode == null) { return null; } Node explpore = head; while(explore == slow) { explore = explore.nextNode; slow = slow.nextNode; } return explore; } public static void main(String[] args) { Node head = new Node(3); head.nextNode = new Node(2); head.nextNode.nextNode = new Node(0); head.nextNode.nextNode.nextNode = new Node(-4); head.nextNode.nextNode.nextNode.nextNode = head.nextNode; System.out.println(beginCycleNode(head).value); System.out.println(beginCycleNodeTwo(head).value); } } ``` 輸出: ```java= 2 2 ``` :::info 討論項目 - 是否能在 O(1) Memory Space 下解決此問題 ::: ## 檢討與改進 ### Interviewer 1. 提出的問題都太淺,缺乏從 Interviewer 角度延伸問題與更深入的討論(換位思考的能力) 2. 可以問題目裡面的演算法是否有在實務中使用過 ### Interviewee 1. 解析問題花費太多時間,應該掌握在 15~30 秒左右說完方法(掌握時間的能力) 2. 若音調能夠再多點變化,更能夠讓 Interviewer 專心聽完 3. 本次使用 hackmd 而非 Google docs coding,下次可採用 google docs 會更有真實感 4. 搭配白板分析問題,能夠更清楚的表達想法 5. 有盡可能在過程中做到 REACTO,但還是會忘記要做 Optimization

    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