-
Notifications
You must be signed in to change notification settings - Fork 29
/
24.swap-nodes-in-pairs.kt
60 lines (57 loc) · 1.21 KB
/
24.swap-nodes-in-pairs.kt
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
56
57
58
59
/*
* @lc app=leetcode id=24 lang=kotlin
*
* [24] Swap Nodes in Pairs
*
* https://leetcode.com/problems/swap-nodes-in-pairs/description/
*
* algorithms
* Medium (45.94%)
* Likes: 1438
* Dislikes: 128
* Total Accepted: 363.8K
* Total Submissions: 779.5K
* Testcase Example: '[1,2,3,4]'
*
* Given a linked list, swap every two adjacent nodes and return its head.
*
* You may not modify the values in the list's nodes, only nodes itself may be
* changed.
*
*
*
* Example:
*
*
* Given 1->2->3->4, you should return the list as 2->1->4->3.
*
*
*/
// @lc code=start
/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun swapPairs(head: ListNode?): ListNode? {
var G: ListNode? = ListNode(0)
G?.next = head
var p = G
var head = head
while (head != null && head.next != null) {
var temp = head?.next
head?.next = temp.next
temp?.next = head
p?.next = temp
p = head
head = head?.next
}
return G?.next
}
}
// @lc code=end