From d6d6a16c7e24d9737d667507a729ec9e60cd1bda Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 10 Jan 2023 00:46:04 +0800 Subject: [PATCH] Update the access() function of linked_list --- .../chapter_array_and_linkedlist/linked_list.cpp | 2 +- .../chapter_array_and_linkedlist/linked_list.cs | 2 +- .../go/chapter_array_and_linkedlist/linked_list.go | 2 +- .../chapter_array_and_linkedlist/linked_list.java | 2 +- .../chapter_array_and_linkedlist/linked_list.py | 2 +- .../chapter_array_and_linkedlist/linked_list.swift | 2 +- docs/chapter_array_and_linkedlist/linked_list.md | 13 +++++++------ 7 files changed, 13 insertions(+), 12 deletions(-) diff --git a/codes/cpp/chapter_array_and_linkedlist/linked_list.cpp b/codes/cpp/chapter_array_and_linkedlist/linked_list.cpp index 13fa6869..5e976a89 100644 --- a/codes/cpp/chapter_array_and_linkedlist/linked_list.cpp +++ b/codes/cpp/chapter_array_and_linkedlist/linked_list.cpp @@ -28,9 +28,9 @@ void remove(ListNode* n0) { /* 访问链表中索引为 index 的结点 */ ListNode* access(ListNode* head, int index) { for (int i = 0; i < index; i++) { - head = head->next; if (head == nullptr) return nullptr; + head = head->next; } return head; } diff --git a/codes/csharp/chapter_array_and_linkedlist/linked_list.cs b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs index d700a5ae..d765724f 100644 --- a/codes/csharp/chapter_array_and_linkedlist/linked_list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs @@ -39,9 +39,9 @@ namespace hello_algo.chapter_array_and_linkedlist { for (int i = 0; i < index; i++) { - head = head.next; if (head == null) return null; + head = head.next; } return head; } diff --git a/codes/go/chapter_array_and_linkedlist/linked_list.go b/codes/go/chapter_array_and_linkedlist/linked_list.go index fa4538ab..8a60fd18 100644 --- a/codes/go/chapter_array_and_linkedlist/linked_list.go +++ b/codes/go/chapter_array_and_linkedlist/linked_list.go @@ -29,10 +29,10 @@ func removeNode(n0 *ListNode) { /* 访问链表中索引为 index 的结点 */ func access(head *ListNode, index int) *ListNode { for i := 0; i < index; i++ { - head = head.Next if head == nil { return nil } + head = head.Next } return head } diff --git a/codes/java/chapter_array_and_linkedlist/linked_list.java b/codes/java/chapter_array_and_linkedlist/linked_list.java index 570778ff..0db8f6ae 100644 --- a/codes/java/chapter_array_and_linkedlist/linked_list.java +++ b/codes/java/chapter_array_and_linkedlist/linked_list.java @@ -29,9 +29,9 @@ public class linked_list { /* 访问链表中索引为 index 的结点 */ static ListNode access(ListNode head, int index) { for (int i = 0; i < index; i++) { - head = head.next; if (head == null) return null; + head = head.next; } return head; } diff --git a/codes/python/chapter_array_and_linkedlist/linked_list.py b/codes/python/chapter_array_and_linkedlist/linked_list.py index dce11034..4fb6b1ba 100644 --- a/codes/python/chapter_array_and_linkedlist/linked_list.py +++ b/codes/python/chapter_array_and_linkedlist/linked_list.py @@ -26,9 +26,9 @@ def remove(n0): """ 访问链表中索引为 index 的结点 """ def access(head, index): for _ in range(index): - head = head.next if not head: return None + head = head.next return head """ 在链表中查找值为 target 的首个结点 """ diff --git a/codes/swift/chapter_array_and_linkedlist/linked_list.swift b/codes/swift/chapter_array_and_linkedlist/linked_list.swift index 06837f75..90218b26 100644 --- a/codes/swift/chapter_array_and_linkedlist/linked_list.swift +++ b/codes/swift/chapter_array_and_linkedlist/linked_list.swift @@ -29,10 +29,10 @@ func remove(n0: ListNode) { func access(head: ListNode, index: Int) -> ListNode? { var head: ListNode? = head for _ in 0 ..< index { - head = head?.next if head == nil { return nil } + head = head?.next } return head } diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 7aa2754c..8dd06978 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -400,6 +400,7 @@ comments: true n0.next = P; P.next = n1; } + /* 删除链表的结点 n0 之后的首个结点 */ function remove(n0: ListNode): void { if (!n0.next) { @@ -474,9 +475,9 @@ comments: true /* 访问链表中索引为 index 的结点 */ ListNode access(ListNode head, int index) { for (int i = 0; i < index; i++) { - head = head.next; if (head == null) return null; + head = head.next; } return head; } @@ -488,9 +489,9 @@ comments: true /* 访问链表中索引为 index 的结点 */ ListNode* access(ListNode* head, int index) { for (int i = 0; i < index; i++) { - head = head->next; if (head == nullptr) return nullptr; + head = head->next; } return head; } @@ -502,9 +503,9 @@ comments: true """ 访问链表中索引为 index 的结点 """ def access(head, index): for _ in range(index): - head = head.next if not head: return None + head = head.next return head ``` @@ -514,10 +515,10 @@ comments: true /* 访问链表中索引为 index 的结点 */ func access(head *ListNode, index int) *ListNode { for i := 0; i < index; i++ { - head = head.Next if head == nil { return nil } + head = head.Next } return head } @@ -566,9 +567,9 @@ comments: true { for (int i = 0; i < index; i++) { - head = head.next; if (head == null) return null; + head = head.next; } return head; } @@ -581,10 +582,10 @@ comments: true func access(head: ListNode, index: Int) -> ListNode? { var head: ListNode? = head for _ in 0 ..< index { - head = head?.next if head == nil { return nil } + head = head?.next } return head }