資料結構 單鏈表基礎面試題 上

2021-10-04 11:15:44 字數 1567 閱讀 8225

//1、單鏈表的逆置

node*

reverse

(node* s1)

}//2、移除鍊錶的元素

//輸入:1->2->6->3->4->5->6 val=6

//輸出:1->2->3->4->5

node*

removeelements

(node* head,

int val)

else

free

(cur)

; cur = next;

} prev = cur;

cur = next;

}return head;

}//3、返回鍊錶的中間節點 //1->2->6->3->4->5->6

node*

middlenode

(node* head)

//法一:遍歷一遍,找中間元素

cur = head;

count = count /2;

while

(count--

)return cur;

}node*

middlenode

(node* head)

//法二:快慢指標法

return slow;

}//4、合併兩個有序鍊錶

//輸入:1->2->4,1->3->4

//輸出:1->1->2->3->4->4

struct listnode*

mergetwolists

(struct listnode* l1,

struct listnode* l2)

if(l1 !=

null

)struct listnode* head;

struct listnode* tail;

if(l1-

>data < l2-

>data)

else

tail = head;

while

(l1&&l2)

else}if

(l1)

else

return head;

}//5、鍊錶分割

//編寫**,以給定值x為基準將鍊錶分割成兩個部分,所有小於x的結點排在

//大於或者等於x的結點之前,給定乙個鍊錶的頭指標listnode* phead

//請返回重新排列後的鍊錶的頭指標。注意:分割後保持原來的資料順序不變

listnode*

partition

(listnode* phead,

int x)

else

cur = cur-

>next;

}//合併兩個有序鍊錶

lesstail-

>_next = greathead-

>next;

greattail-

>next =

null

; phead = lesshead-

>next;

return phead;

}

鏈表面試題(上)

slistnode.h ifndef slistnode h define slistnode h include include include include includetypedef int datatype typedef struct slistnode slistnode slist...

鏈表面試題(上)

slistnode.h ifndef slistnode h define slistnode h include include include include includetypedef int datatype typedef struct slistnode slistnode slist...

資料結構之單鏈表(二)面試題

單鏈表的反轉 singlelinkedlistnode prev head.next singlelinkedlistnode pcur prev.next while prev.next null pcur是要操作的節點,prev是其前置節點,初始為prev為第乙個有效節點,pcur為第二個有效節...