We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5a088d3 commit bd63483Copy full SHA for bd63483
solution/024.Swap Nodes in Pairs/Solution.cpp
@@ -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
26
27
+ tail->next=tw;
28
+ tail=head;
29
30
+ }
31
+ return real_head;
32
33
34
+};
0 commit comments