華為oj 輸出單向鍊錶中倒數第k個結點

2021-07-10 05:29:21 字數 496 閱讀 5604

終於到這道題了,這是俺們那年考研資料結構最後一道題!

思路:設定相距為k的兩個指標,當後乙個指標指向null 時,前面的指標就指向了倒數第k個,程式如下:

#include using namespace std;

struct listnode

;int main()

cin >> k;

p = q = head->m_pnext;

for (int i = 0; i < k; i++)

while (q->m_pnext != null)

cout << p->m_nkey<

return 0;

}

結論:還有找到中間節點的思路,也設定兩個指標,乙個指標每次++,另乙個指標進行兩次++!!!

華為OJ 輸出單向鍊錶中倒數第k個結點

輸入乙個單向鍊錶,輸出該鍊錶中倒數第k個結點,鍊錶的倒數第0個結點為鍊錶的尾指標。鍊錶結點定義如下 struct listnode int m nkey listnode m pnext 詳細描述 介面說明 原型 listnode findkthtotail listnode plisthead,u...

華為OJ 初級 輸出單向鍊錶中倒數第k個結點

輸入乙個單向鍊錶,輸出該鍊錶中倒數第k個結點,鍊錶的倒數第0個結點為鍊錶的尾指標。鍊錶結點定義如下 struct listnode 詳細描述 介面說明 原型 listnode findkthtotail listnode plisthead,unsignedint k 輸入引數 listnode p...

輸出單向鍊錶中倒數第k個結點

描述 輸入乙個單向鍊錶,輸出該鍊錶中倒數第k個結點,鍊錶的倒數第0個結點為鍊錶的尾指標。鍊錶結點定義如下 struct listnode 詳細描述 介面說明 原型 listnode findkthtotail listnode plisthead,unsignedint k 輸入引數 listnod...