24. Swap Nodes in Pairs(Linked List)
来源:互联网 发布:淘宝客服是卖家吗 编辑:程序博客网 时间:2024/05/18 13:08
Title
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.
Language C
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* swapPairs(struct ListNode* head) { if(head == NULL || head->next == NULL){ return head; } struct ListNode *p, *q, *h, *pre; // h = (struct ListNode *)malloc(sizeof(struct ListNode)); // h->val =0; h->next = head; p = head; pre = h; q = p->next; while(p != NULL && q != NULL){//surprised that must have pre //This isn't working : p->next = q->next;q->next = p; pre->next = q; p->next = q->next; q->next = p; pre = pre->next->next; p = p->next; q = p->next; } return h->next;}
runtime:2ms
0 0
- 24. Swap Nodes in Pairs(Linked List)
- 24. Swap Nodes in Pairs leetcode(list)
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 345. Reverse Vowels of a String
- Linux command:chmod
- Memcached学习使用
- 主要是对于音频的频率 和 采样的采样率 之间的疑惑的 整理
- Programming Scala第4章demo04 伴生对象和伴生类
- 24. Swap Nodes in Pairs(Linked List)
- css display table-cell
- java ArrayList自动排序算法的实现
- 从相册或者相机获得uri转为bitmap
- 【VS开发】字符,字节和编码
- 基于FS4412嵌入式系统移植(6) glib库的交叉编译与移植
- 从eclipse配置tomcat多实例了解tomcat的多实例
- Programming Scala第4章demo05 通过伴生对象完成静态字段与方法的访问
- 虚拟机Ubuntu server 14.04 安装 virtualbox 增强包