Update the access() function of linked_list

This commit is contained in:
Yudong Jin 2023-01-10 00:46:04 +08:00
parent aaa2ff29f9
commit d6d6a16c7e
7 changed files with 13 additions and 12 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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
}

View File

@ -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;
}

View File

@ -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 的首个结点 """

View File

@ -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
}

View File

@ -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
}