File tree 4 files changed +170
-0
lines changed
solution/0000-0099/0024.Swap Nodes in Pairs
4 files changed +170
-0
lines changed Original file line number Diff line number Diff line change @@ -256,6 +256,34 @@ var swapPairs = function (head) {
256
256
};
257
257
```
258
258
259
+ #### C#
260
+
261
+ ``` cs
262
+ /**
263
+ * Definition for singly-linked list.
264
+ * public class ListNode {
265
+ * public int val;
266
+ * public ListNode next;
267
+ * public ListNode(int val=0, ListNode next=null) {
268
+ * this.val = val;
269
+ * this.next = next;
270
+ * }
271
+ * }
272
+ */
273
+ public class Solution {
274
+ public ListNode SwapPairs (ListNode head ) {
275
+ if (head is null || head .next is null ) {
276
+ return head ;
277
+ }
278
+ ListNode t = SwapPairs (head .next .next );
279
+ ListNode p = head .next ;
280
+ p .next = head ;
281
+ head .next = t ;
282
+ return p ;
283
+ }
284
+ }
285
+ ```
286
+
259
287
#### Ruby
260
288
261
289
``` rb
@@ -466,6 +494,38 @@ var swapPairs = function (head) {
466
494
};
467
495
```
468
496
497
+ #### C#
498
+
499
+ ``` cs
500
+ /**
501
+ * Definition for singly-linked list.
502
+ * public class ListNode {
503
+ * public int val;
504
+ * public ListNode next;
505
+ * public ListNode(int val=0, ListNode next=null) {
506
+ * this.val = val;
507
+ * this.next = next;
508
+ * }
509
+ * }
510
+ */
511
+ public class Solution {
512
+ public ListNode SwapPairs (ListNode head ) {
513
+ ListNode dummy = new ListNode (0 , head );
514
+ ListNode pre = dummy ;
515
+ ListNode cur = head ;
516
+ while (cur is not null && cur .next is not null ) {
517
+ ListNode t = cur .next ;
518
+ cur .next = t .next ;
519
+ t .next = cur ;
520
+ pre .next = t ;
521
+ pre = cur ;
522
+ cur = cur .next ;
523
+ }
524
+ return dummy .next ;
525
+ }
526
+ }
527
+ ```
528
+
469
529
#### PHP
470
530
471
531
``` php
Original file line number Diff line number Diff line change @@ -269,6 +269,34 @@ var swapPairs = function (head) {
269
269
};
270
270
```
271
271
272
+ #### C#
273
+
274
+ ``` cs
275
+ /**
276
+ * Definition for singly-linked list.
277
+ * public class ListNode {
278
+ * public int val;
279
+ * public ListNode next;
280
+ * public ListNode(int val=0, ListNode next=null) {
281
+ * this.val = val;
282
+ * this.next = next;
283
+ * }
284
+ * }
285
+ */
286
+ public class Solution {
287
+ public ListNode SwapPairs (ListNode head ) {
288
+ if (head is null || head .next is null ) {
289
+ return head ;
290
+ }
291
+ ListNode t = SwapPairs (head .next .next );
292
+ ListNode p = head .next ;
293
+ p .next = head ;
294
+ head .next = t ;
295
+ return p ;
296
+ }
297
+ }
298
+ ```
299
+
272
300
#### Ruby
273
301
274
302
``` rb
@@ -479,6 +507,38 @@ var swapPairs = function (head) {
479
507
};
480
508
```
481
509
510
+ #### C#
511
+
512
+ ``` cs
513
+ /**
514
+ * Definition for singly-linked list.
515
+ * public class ListNode {
516
+ * public int val;
517
+ * public ListNode next;
518
+ * public ListNode(int val=0, ListNode next=null) {
519
+ * this.val = val;
520
+ * this.next = next;
521
+ * }
522
+ * }
523
+ */
524
+ public class Solution {
525
+ public ListNode SwapPairs (ListNode head ) {
526
+ ListNode dummy = new ListNode (0 , head );
527
+ ListNode pre = dummy ;
528
+ ListNode cur = head ;
529
+ while (cur is not null && cur .next is not null ) {
530
+ ListNode t = cur .next ;
531
+ cur .next = t .next ;
532
+ t .next = cur ;
533
+ pre .next = t ;
534
+ pre = cur ;
535
+ cur = cur .next ;
536
+ }
537
+ return dummy .next ;
538
+ }
539
+ }
540
+ ```
541
+
482
542
#### PHP
483
543
484
544
``` php
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * public int val;
5
+ * public ListNode next;
6
+ * public ListNode(int val=0, ListNode next=null) {
7
+ * this.val = val;
8
+ * this.next = next;
9
+ * }
10
+ * }
11
+ */
12
+ public class Solution {
13
+ public ListNode SwapPairs ( ListNode head ) {
14
+ if ( head is null || head . next is null ) {
15
+ return head ;
16
+ }
17
+ ListNode t = SwapPairs ( head . next . next ) ;
18
+ ListNode p = head . next ;
19
+ p . next = head ;
20
+ head . next = t ;
21
+ return p ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * public int val;
5
+ * public ListNode next;
6
+ * public ListNode(int val=0, ListNode next=null) {
7
+ * this.val = val;
8
+ * this.next = next;
9
+ * }
10
+ * }
11
+ */
12
+ public class Solution {
13
+ public ListNode SwapPairs ( ListNode head ) {
14
+ ListNode dummy = new ListNode ( 0 , head ) ;
15
+ ListNode pre = dummy ;
16
+ ListNode cur = head ;
17
+ while ( cur is not null && cur . next is not null ) {
18
+ ListNode t = cur . next ;
19
+ cur . next = t . next ;
20
+ t . next = cur ;
21
+ pre . next = t ;
22
+ pre = cur ;
23
+ cur = cur . next ;
24
+ }
25
+ return dummy . next ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments