演算法系列 Swap Nodes in Pairs

2021-09-30 13:43:55 字數 489 閱讀 9154

given a linked list, swap every two adjacent nodes and return its head.

for example,given 1->2->3->4, you should return the list as 2->1->4->3.

your algorithm should use only constant space. you may not modify the values in the list, only nodes itself can be changed.

這個題目稍微複雜一點。如下圖所示,我們引入虛擬頭結點,並且利用4個指標p,node1,node2,next,分別表示 描述 前驅,交換結點1,交換結點2和後繼。更新這四個結點的值即可。

public

class solution

return dummyhead.next;}}

java演算法系列

棧的概念 棧是一種特殊的線性表,堆疊的資料元素以及資料元素之間的關係和線性表是完全一樣的。差別是線性表是在任意位置進行插入和刪除操作,棧是只允許在固定的一端進行插入和刪除,棧的插入和刪除只允許在棧頂,棧的插入和刪除通常稱為進棧和出棧。資料集合 每個資料元素的資料型別可以是任意的型別 操作的集合 進棧...

演算法系列 Move Zeroes

given an array nums,write a function to move all 0 s to the end of it while maintaining the relative order of the non zero elements.for example,given ...

演算法系列 Missing Number

given an array containing n distinct numbers taken from 0,1,2,n,find the one that is missing from the array.for example,given nums 0,1,3 return 2.note...