首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从单链表的末尾找到第n个元素?

如何从单链表的末尾找到第n个元素?
EN

Stack Overflow用户
提问于 2010-04-08 16:03:57
回答 29查看 99.5K关注 0票数 65

下面的函数尝试查找nth to 单链表的最后一个元素。

例如:

如果元素为8->10->5->7->2->1->5->4->10->10,则结果为7th to last node is 7

有没有人能帮我讲讲这段代码是如何工作的,或者有没有更好更简单的方法?

代码语言:javascript
运行
复制
LinkedListNode nthToLast(LinkedListNode head, int n) {
  if (head == null || n < 1) {
    return null;
  }

  LinkedListNode p1 = head;
  LinkedListNode p2 = head;

  for (int j = 0; j < n - 1; ++j) { // skip n-1 steps ahead
    if (p2 == null) {
      return null; // not found since list size < n
    }
    p2 = p2.next;
  }

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

  return p1;
}
EN

回答 29

Stack Overflow用户

回答已采纳

发布于 2010-04-08 16:17:06

您的算法首先创建对链表中相隔N个节点的两个节点的引用。因此,在您的示例中,如果N为7,则会将p1设置为8,将p2设置为4。

然后,它会将每个节点引用前进到列表中的下一个节点,直到p2到达列表中的最后一个元素。同样,在您的示例中,这将是当p1为5,p2为10时。此时,p1引用列表中倒数第N个元素(通过它们相距N个节点的属性)。

票数 37
EN

Stack Overflow用户

发布于 2010-04-09 13:07:41

该算法的关键是最初通过n-1节点将两个指针p1p2分开,因此我们希望p2从列表的开头指向(n-1)th节点,然后移动p2,直到它到达列表的last节点。一旦p2到达列表的末尾,p1将指向列表末尾的第n个节点。

我已经将解释内联为注释。希望它能有所帮助:

代码语言:javascript
运行
复制
// Function to return the nth node from the end of a linked list.
// Takes the head pointer to the list and n as input
// Returns the nth node from the end if one exists else returns NULL.
LinkedListNode nthToLast(LinkedListNode head, int n) {
  // If list does not exist or if there are no elements in the list,return NULL
  if (head == null || n < 1) {
    return null;
  }

  // make pointers p1 and p2 point to the start of the list.
  LinkedListNode p1 = head;
  LinkedListNode p2 = head;

  // The key to this algorithm is to set p1 and p2 apart by n-1 nodes initially
  // so we want p2 to point to the (n-1)th node from the start of the list
  // then we move p2 till it reaches the last node of the list. 
  // Once p2 reaches end of the list p1 will be pointing to the nth node 
  // from the end of the list.

  // loop to move p2.
  for (int j = 0; j < n - 1; ++j) { 
   // while moving p2 check if it becomes NULL, that is if it reaches the end
   // of the list. That would mean the list has less than n nodes, so its not 
   // possible to find nth from last, so return NULL.
   if (p2 == null) {
       return null; 
   }
   // move p2 forward.
   p2 = p2.next;
  }

  // at this point p2 is (n-1) nodes ahead of p1. Now keep moving both forward
  // till p2 reaches the last node in the list.
  while (p2.next != null) {
    p1 = p1.next;
    p2 = p2.next;
  }

   // at this point p2 has reached the last node in the list and p1 will be
   // pointing to the nth node from the last..so return it.
   return p1;
 }

或者,我们可以将p1p2设置为n个节点,而不是(n-1),然后将p2移动到列表的末尾,而不是移动到最后一个节点:

代码语言:javascript
运行
复制
LinkedListNode p1 = head;
LinkedListNode p2 = head;
for (int j = 0; j < n ; ++j) { // make then n nodes apart.
    if (p2 == null) {
        return null;
    }
    p2 = p2.next;
}
while (p2 != null) { // move till p2 goes past the end of the list.
    p1 = p1.next;
    p2 = p2.next;
}
return p1;
票数 68
EN

Stack Overflow用户

发布于 2010-08-15 04:20:25

你对这种方法有什么看法?

linkedlist.

  • Actual

  • Count linkedlist.

  • Actual节点索引的长度=
  1. length -给定索引;
  2. 编写一个函数从head遍历并获取上述索引处的节点。
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2598348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档