Skip to content

Commit bd63483

Browse files
author
xiapengchng
committed
Update solution 024, adding cpp solution version
1 parent 5a088d3 commit bd63483

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* swapPairs(ListNode* head) {
12+
if(head==NULL||head->next==NULL)
13+
return head;
14+
ListNode *real_head=head->next;
15+
16+
17+
ListNode * tw=head->next;
18+
head->next=head->next->next;
19+
tw->next=head;
20+
ListNode *tail=head;
21+
head=tw->next->next;
22+
while(head!=NULL&&head->next!=NULL)
23+
{
24+
tw=head->next;
25+
head->next=head->next->next;
26+
tw->next=head;
27+
tail->next=tw;
28+
tail=head;
29+
head=tw->next->next;
30+
}
31+
return real_head;
32+
}
33+
34+
};

0 commit comments

Comments
 (0)