leetcode 518,[leetcode] 24. Swap Nodes in Pairs

 2023-10-17 阅读 29 评论 0

摘要:題目 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 lis

題目

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.
Subscribe to see which companies asked this question

描述

讀題只能改鏈表節點里的值,那么這題不是很簡單嗎?直接交換就可以了

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* swapPairs(struct ListNode* head) {struct ListNode *p, *pnext;int tmp;p = head;while(p && p->next){tmp = p->val;p->val = p->next->val;p->next->val = tmp;p = p->next->next;}return head;
}

Accept,不知道這道題想考察什么~~

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/143578.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息