-
Notifications
You must be signed in to change notification settings - Fork 57
/
Solution.py
96 lines (76 loc) · 2.03 KB
/
Solution.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
You are given the head of a linked list, and an integer k.
Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]
Example 2:
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
Output: [7,9,6,6,8,7,3,0,9,5]
Example 3:
Input: head = [1], k = 1
Output: [1]
Example 4:
Input: head = [1,2], k = 1
Output: [2,1]
Example 5:
Input: head = [1,2,3], k = 2
Output: [1,2,3]
Constraints:
The number of nodes in the list is n.
1 <= k <= n <= 105
0 <= Node.val <= 100
"""
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapNodes(self, head: ListNode, k: int) -> ListNode:
def get_k_from_head(h, k):
p, c = h, h.next
while k > 1:
p = c
c = c.next
k -= 1
return p, c
def get_k_from_tail(h, k):
f = h.next
while k > 0:
f = f.next
k -= 1
p, c = h, h.next
while f is not None:
p = c
c = c.next
f = f.next
return p, c
if head is None or head.next is None:
return head
nh = ListNode()
nh.next = head
p1, c1 = get_k_from_head(nh, k)
p2, c2 = get_k_from_tail(nh, k)
if c1 == c2:
return nh.next
if p1 == c2:
p2.next = c1
c1_next = c1.next
c1.next = c2
c2.next = c1_next
return nh.next
if p2 == c1:
p1.next = c2
c2_next = c2.next
c2.next = c1
c1.next = c2_next
return nh.next
c2_next = c2.next
c2.next = c1.next
c1.next = c2_next
p1.next = c2
p2.next = c1
return nh.next