forked from algorithm001/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_24_2.cs
More file actions
55 lines (48 loc) · 1.25 KB
/
LeetCode_24_2.cs
File metadata and controls
55 lines (48 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace leetcode.两两交换链表中的节点
{
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
public class 两两交换链表中的节点
{
public ListNode SwapPairs(ListNode head)
{
ListNode a = new ListNode(int.MinValue);
ListNode b = head;
ListNode c = head?.next;
a.next = head;
head = a;
while (b != null && c != null)
{
var temp = c.next;
c.next = b;
b.next = temp;
a.next = c;
a = b;
b = b?.next;
c = b?.next;
}
head = head.next;
return head;
}
public ListNode SwapPairs_recursive(ListNode head)
{
if (head == null || head.next == null)
{
return head;
}
var next = head.next;
head.next = SwapPairs_recursive(next.next);
next.next = head;
return next;
}
}
}