寻找一个单项链表的中项,如果存在两个则返回前一个。请给出算法描述并给出代码实现。

系统消息 若觉得内容不错,请点击左上角的"赞"图标,以优化网站的内容呈现。 另外,请及时验证注册邮箱,否则收不到21QA发出的红包。 官方Q群:250203055

asked 14 Sep '14, 18:13

www%E7%82%B921qa%E7%82%B9net's gravatar image

www点21qa点net
717813

编辑于 16 Sep '14, 09:10

%E5%8E%9F%E9%87%8E%E4%B9%8B%E7%8B%BC's gravatar image

原野之狼
1.9k9399116


head = GetList()
tail = head
mid = head
i = 0
while tail.next != NULL && tail.next != head:
  tail = tail.next
  i += 1
  if i % 2 == 0:
    mid = mid.next
if i % 2 == 1:
  return [mid, mid.next]
else:
  return [mid]
系统消息 若觉得内容不错,请点击左上角的"赞"图标,以优化网站的内容呈现。 另外,请及时验证注册邮箱,否则收不到21QA发出的红包。 官方Q群:250203055
permanent link

answered 16 Sep '14, 09:43

%E4%BD%A0%E6%9C%AC%E5%A6%82%E5%8E%BB's gravatar image

你本如去
1866

struct node* get_mid_node(struct node* head)
{
    struct node * mid = head;

    if (head == NULL) {
        return NULL;
    }

    while ( head->next != NULL) {
        head = head->next;
        if (head->next == NULL) {
            break;
        }
        head = head->next;
        mid = mid->next;
    }

    return mid;
}
系统消息 若觉得内容不错,请点击左上角的"赞"图标,以优化网站的内容呈现。 另外,请及时验证注册邮箱,否则收不到21QA发出的红包。 官方Q群:250203055
permanent link

answered 17 Sep '14, 09:51

%E5%8E%9F%E9%87%8E%E4%B9%8B%E7%8B%BC's gravatar image

原野之狼
1.9k9399116

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link: [text](http://url.com/ "title")
  • image: ![alt](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×33
×14
×5

question asked: 14 Sep '14, 18:13

question was seen: 2,865 times

last updated: 17 Sep '14, 09:51

powered by O*S*Q*A

粤ICP备14040061号-1