NC78-反转列表
描述
輸入一個鏈表,反轉鏈表后,輸出新鏈表的表頭。
示例1
輸入:
{1,2,3}返回值:
{3,2,1}代碼:
struct ListNode* ReverseList(struct ListNode* phead ) {// write code here /* 不帶頭結點的struct ListNode *newlist;newlist=NULL;struct ListNode *p;while(phead!=NULL){p=phead->next;phead->next=newlist;newlist=phead;phead=p;}return newlist; *///帶頭結點的struct ListNode* newlist=(struct ListNode*) malloc(sizeof(struct ListNode));newlist->next=NULL;struct ListNode*p;while(phead){p=phead->next;phead->next=newlist->next;newlist->next=phead;phead=p;}return newlist->next;}總結
- 上一篇: 牛客网NC112--进制转换
- 下一篇: Nc105-二分查找-II