leetcode : Swap Nodes in Pairs
runtime 0 ms, beats 100% of cpp submissions
O(n) solution with explanation
tags: linked list, pointer
๐ link
๐ description
็ตฆๅฎไธๅ linked list ๏ผๅฐ็ธ้ฐ็ๅ ฉๅ้ป็ฟป่ฝใ
ex. 1 -> 2 -> 3 -> 4 2 -> 1 -> 4 -> 3 1ใ2ไบๆ๏ผ3ใ4ไบๆ
๐ง solution
ๆ นๆ้กๆๆจกๆฌๅณๅฏ๏ผไฝฟ็จๅ ฉๅ pointer ๅๅฅๆๅ้่ฆไบๆ็ๅ ฉๅ้ป๏ผไบๆๅฎๅพๅๅๅพ็งปๅๅ ฉๆ ผใ
โณ time complexity
้ๆญทไธๆฌก list ๏ผๆ้่ค้ๅบฆ O(n)
็ธฝๆ้่ค้ๅบฆ O(n)
๐ code
1 | class Solution { |