Update the access() function of linked_list
This commit is contained in:
parent
aaa2ff29f9
commit
d6d6a16c7e
@ -28,9 +28,9 @@ void remove(ListNode* n0) {
|
|||||||
/* 访问链表中索引为 index 的结点 */
|
/* 访问链表中索引为 index 的结点 */
|
||||||
ListNode* access(ListNode* head, int index) {
|
ListNode* access(ListNode* head, int index) {
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index; i++) {
|
||||||
head = head->next;
|
|
||||||
if (head == nullptr)
|
if (head == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
head = head->next;
|
||||||
}
|
}
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
@ -39,9 +39,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < index; i++)
|
for (int i = 0; i < index; i++)
|
||||||
{
|
{
|
||||||
head = head.next;
|
|
||||||
if (head == null)
|
if (head == null)
|
||||||
return null;
|
return null;
|
||||||
|
head = head.next;
|
||||||
}
|
}
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
@ -29,10 +29,10 @@ func removeNode(n0 *ListNode) {
|
|||||||
/* 访问链表中索引为 index 的结点 */
|
/* 访问链表中索引为 index 的结点 */
|
||||||
func access(head *ListNode, index int) *ListNode {
|
func access(head *ListNode, index int) *ListNode {
|
||||||
for i := 0; i < index; i++ {
|
for i := 0; i < index; i++ {
|
||||||
head = head.Next
|
|
||||||
if head == nil {
|
if head == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
head = head.Next
|
||||||
}
|
}
|
||||||
return head
|
return head
|
||||||
}
|
}
|
||||||
|
@ -29,9 +29,9 @@ public class linked_list {
|
|||||||
/* 访问链表中索引为 index 的结点 */
|
/* 访问链表中索引为 index 的结点 */
|
||||||
static ListNode access(ListNode head, int index) {
|
static ListNode access(ListNode head, int index) {
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index; i++) {
|
||||||
head = head.next;
|
|
||||||
if (head == null)
|
if (head == null)
|
||||||
return null;
|
return null;
|
||||||
|
head = head.next;
|
||||||
}
|
}
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
@ -26,9 +26,9 @@ def remove(n0):
|
|||||||
""" 访问链表中索引为 index 的结点 """
|
""" 访问链表中索引为 index 的结点 """
|
||||||
def access(head, index):
|
def access(head, index):
|
||||||
for _ in range(index):
|
for _ in range(index):
|
||||||
head = head.next
|
|
||||||
if not head:
|
if not head:
|
||||||
return None
|
return None
|
||||||
|
head = head.next
|
||||||
return head
|
return head
|
||||||
|
|
||||||
""" 在链表中查找值为 target 的首个结点 """
|
""" 在链表中查找值为 target 的首个结点 """
|
||||||
|
@ -29,10 +29,10 @@ func remove(n0: ListNode) {
|
|||||||
func access(head: ListNode, index: Int) -> ListNode? {
|
func access(head: ListNode, index: Int) -> ListNode? {
|
||||||
var head: ListNode? = head
|
var head: ListNode? = head
|
||||||
for _ in 0 ..< index {
|
for _ in 0 ..< index {
|
||||||
head = head?.next
|
|
||||||
if head == nil {
|
if head == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
head = head?.next
|
||||||
}
|
}
|
||||||
return head
|
return head
|
||||||
}
|
}
|
||||||
|
@ -400,6 +400,7 @@ comments: true
|
|||||||
n0.next = P;
|
n0.next = P;
|
||||||
P.next = n1;
|
P.next = n1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除链表的结点 n0 之后的首个结点 */
|
/* 删除链表的结点 n0 之后的首个结点 */
|
||||||
function remove(n0: ListNode): void {
|
function remove(n0: ListNode): void {
|
||||||
if (!n0.next) {
|
if (!n0.next) {
|
||||||
@ -474,9 +475,9 @@ comments: true
|
|||||||
/* 访问链表中索引为 index 的结点 */
|
/* 访问链表中索引为 index 的结点 */
|
||||||
ListNode access(ListNode head, int index) {
|
ListNode access(ListNode head, int index) {
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index; i++) {
|
||||||
head = head.next;
|
|
||||||
if (head == null)
|
if (head == null)
|
||||||
return null;
|
return null;
|
||||||
|
head = head.next;
|
||||||
}
|
}
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
@ -488,9 +489,9 @@ comments: true
|
|||||||
/* 访问链表中索引为 index 的结点 */
|
/* 访问链表中索引为 index 的结点 */
|
||||||
ListNode* access(ListNode* head, int index) {
|
ListNode* access(ListNode* head, int index) {
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index; i++) {
|
||||||
head = head->next;
|
|
||||||
if (head == nullptr)
|
if (head == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
head = head->next;
|
||||||
}
|
}
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
@ -502,9 +503,9 @@ comments: true
|
|||||||
""" 访问链表中索引为 index 的结点 """
|
""" 访问链表中索引为 index 的结点 """
|
||||||
def access(head, index):
|
def access(head, index):
|
||||||
for _ in range(index):
|
for _ in range(index):
|
||||||
head = head.next
|
|
||||||
if not head:
|
if not head:
|
||||||
return None
|
return None
|
||||||
|
head = head.next
|
||||||
return head
|
return head
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -514,10 +515,10 @@ comments: true
|
|||||||
/* 访问链表中索引为 index 的结点 */
|
/* 访问链表中索引为 index 的结点 */
|
||||||
func access(head *ListNode, index int) *ListNode {
|
func access(head *ListNode, index int) *ListNode {
|
||||||
for i := 0; i < index; i++ {
|
for i := 0; i < index; i++ {
|
||||||
head = head.Next
|
|
||||||
if head == nil {
|
if head == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
head = head.Next
|
||||||
}
|
}
|
||||||
return head
|
return head
|
||||||
}
|
}
|
||||||
@ -566,9 +567,9 @@ comments: true
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < index; i++)
|
for (int i = 0; i < index; i++)
|
||||||
{
|
{
|
||||||
head = head.next;
|
|
||||||
if (head == null)
|
if (head == null)
|
||||||
return null;
|
return null;
|
||||||
|
head = head.next;
|
||||||
}
|
}
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
@ -581,10 +582,10 @@ comments: true
|
|||||||
func access(head: ListNode, index: Int) -> ListNode? {
|
func access(head: ListNode, index: Int) -> ListNode? {
|
||||||
var head: ListNode? = head
|
var head: ListNode? = head
|
||||||
for _ in 0 ..< index {
|
for _ in 0 ..< index {
|
||||||
head = head?.next
|
|
||||||
if head == nil {
|
if head == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
head = head?.next
|
||||||
}
|
}
|
||||||
return head
|
return head
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user