LC 交換鍊錶節點

2021-10-17 09:03:03 字數 614 閱讀 9502

/*將給定的鍊錶中每兩個相鄰的節點交換一次,返回鍊錶的頭指標

例如,給出1->2->3->4,你應該返回鍊錶2->1->4->3。

你給出的演算法只能使用常量級的空間。你不能修改列表中的值,只能修改節點本身。*/

struct listnode };

class

solution

listnode* phead =

newlistnode(0

);phead-

>next = head;

listnode* pnode = head;

int count =0;

while

(pnode !=

nullptr

) pnode = head;

listnode* pre = phead;

for(

int i =

0; i <

(count /2)

;++i)

pre = pnode;

pnode = pnode-

>next;

}return phead-

>next;}}

;

lc 刪除鍊錶中的節點

題目很簡單!就是理解題意有點,emmmmm.就想著沒傳入head鍊錶怎麼個刪除法,都不能知道被刪除元素前面的指標,所以就不能通過更改next指標來刪除。只能通過把被刪除節點後面的元素往前移動,並刪除最後乙個節點即可。definition for singly linked list.struct l...

(鍊錶)LC環形鍊錶

使用雜湊表記錄已經走過節點,如果遍歷到已經訪問的,則說明有環 public boolean hascycle listnode head return false 對於判斷鍊錶 陣列是否有環,可以使用floyd判環演算法 龜兔賽跑演算法 使用快慢指標,快指標每次走兩步,慢指標每次走一步,如果快指標走...

交換兩個鍊錶節點

在鍊錶有關的問題當中,鍊錶節點交換是乙個非常 典型的一道題目,先給 後總結。include using namespace std struct node node create int n,int data return head void display node head cout endl ...