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,不知道這道題想考察什么~~
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态