简体   繁体   English

遍历链接列表

[英]Iterating through Linked List

Is there a linear way to find the middle element of a singly linked list? 有线性方法来查找单链表的中间元素吗? (Linear - Meaning you can only iterate through the list once AKA the number of iterations you do total cannot exceed the length of the list) (线性-意味着您只能在列表中迭代一次,也就是说,您所做的总迭代次数不能超过列表的长度)

Thanks! 谢谢!

Edit: The question specifies that you cannot know the length of the list beforehand. 编辑:问题指定您无法事先知道列表的长度。

Edit 2: The question is written for Java, but using some linked list definition that does not have a length() method 编辑2:该问题是为Java编写的,但是使用了没有length()方法的某些链表定义

This might not fit your constraints, but they're quite vague. 这可能不适合您的约束,但是它们很模糊。 It only requires a single iteration of the list (in the sense of starting at the beginning only once, and reaching the end only once), but it requires two independent pointers being stored as you do so (and therefore follows half of the list's next pointers twice). 它只需要列表的一次迭代(就意味着从头开始仅一次,而到达结束仅一次),但是这样做时需要存储两个独立的指针(因此紧随列表的下一个一半)指针两次)。

  1. Set pointer_1 and pointer_2 to the first node in the list, and counter to 0 将指针_1和指针_2设置到列表中的第一个节点,并将计数器设置为0
  2. Set pointer_1 to pointer_1->next 将指针_1设置为指针_1->下一个
  3. Increment counter 增量计数器
  4. If counter is even, set pointer_2 to pointer_2->next 如果计数器为偶数,则将指针_2设置为指针_2->下一个
  5. If pointer_1 is not at the end of the list, goto 2 如果指针_1不在列表的末尾,请转到2
  6. When the loop exits, pointer_2 is at the middle of the list 循环退出时,指针_2位于列表的中间

Use two variables that point to the first element of your list. 使用两个变量指向列表的第一个元素。 Then iterate through the list, incrementing the first pointer by 1 place and the second pointer by 2 places on each iteration. 然后遍历列表,每次迭代将第一个指针增加1个位置,将第二个指针增加2个位置。 Once the second pointer reaches the end of the list, the first pointer will contain the middle element (if it exists). 一旦第二个指针到达列表的末尾,第一个指针将包含中间元素(如果存在)。

A bit of pseudo code... 一点伪代码...

list = [1, 2, 3, 4, 5]
ptr1 = list[0]
ptr2 = list[0]
while ptr2 is not null
    ptr1 = ptr1.next
    ptr2 = ptr2.next.next
return ptr1
Node ptr1 = head; 
Node ptr2 = head;

while(ptr1 != null || ptr1->next != null){
    ptr1 = ptr1 -> next -> next;
    ptr2 = ptr2 -> next; 
}

EDIT: 编辑:

//this is wrong, so not needed so, upper part is enough. 
if(ptr1->next != null){
    ptr2 = ptr2 -> next;
}
Node p1 = head;
Node mid = head;

while(p1 != null){
    p1 = p1.next();
    p1 = p1.next();
    mid = mid.next(); 
}

Be aware, the order matters with p1 and mid in the loop. 请注意,顺序与p1和循环中的中间位置有关。 Take for instance if you had only one element in the list. 例如,如果列表中只有一个元素。 That means the head would be the middle. 这意味着头部将在中间。 However, mid would return null because it went to the next element, instead of exiting the loop before getting to the next element. 但是, mid将返回null因为它转到了下一个元素,而不是在到达下一个元素之前退出了循环。

WRONG IMPLEMENTATION 错误执行

while(p1 != null){
    mid = mid.next(); // incorrect order.
    p1 = p1.next().next(); //will cause exception if p1.next == null
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM