92 反轉鍊錶 II

2021-09-01 23:32:11 字數 660 閱讀 3312

反轉從位置 m 到 n 的鍊錶。請使用一趟掃瞄完成反轉。

說明:

1 ≤ m ≤ n ≤ 鍊錶長度。

示例:

輸入:1->2->3->4->5->null, m = 2, n = 4輸出:1->4->3->2->5->null
/**

* definition for singly-linked list.

* struct listnode

* };

*/class solution //此時head到達逆置段頭節點,

listnode *modify_list_tail = head;//將modify指向逆置段頭節點,也就是逆置後的尾結點

listnode *new_head = null;

while(head && change_len)

//此時head到達逆置段後接節點

modify_list_tail->next = head;//將逆置完成段與後接節點連線。

if(pre_head)

else

return result;

}};

92 反轉鍊錶 II

題目描述 反轉從位置 m 到 n 的鍊錶。請使用一趟掃瞄完成反轉。說明 1 m n 鍊錶長度。示例 輸入 1 2 3 4 5 null,m 2,n 4 輸出 1 4 3 2 5 null 方法1 主要思路 1 直觀的想,找出要反轉的一段的鍊錶的頭乙個結點的前乙個結點,使用頭插法,將該段鍊錶中的結點,...

92 反轉鍊錶 II

92.反轉鍊錶 ii 難度中等425收藏分享切換為英文關注反饋 反轉從位置 m 到 n 的鍊錶。請使用一趟掃瞄完成反轉。說明 1 m n 鍊錶長度。示例 輸入 1 2 3 4 5 null,m 2,n 4輸出 1 4 3 2 5 nullpublic static listnode reverseb...

92 反轉鍊錶 II

92.反轉鍊錶 ii 雙指標頭插法 1.definition for singly linked list.2.public class listnode 6.class solution for int i 0 i n m i return dummyhead.next 其他 為什麼用dummyh...