File tree Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 48ms 71.90%
2
+
3
+ # Definition for singly-linked list.
4
+ # class ListNode:
5
+ # def __init__(self, x):
6
+ # self.val = x
7
+ # self.next = None
8
+
9
+ class Solution :
10
+ def rotateRight (self , head , k ):
11
+ """
12
+ :type head: ListNode
13
+ :type k: int
14
+ :rtype: ListNode
15
+ """
16
+ if not head :
17
+ return []
18
+
19
+ stack = []
20
+ len_listnode = 0
21
+ mid = head
22
+ while mid :
23
+ stack .append (mid )
24
+ mid = mid .next
25
+ len_listnode += 1
26
+
27
+ k = k - k // len_listnode * len_listnode
28
+
29
+ if k is 0 :
30
+ return head
31
+
32
+ stack [- 1 ].next = stack [0 ]
33
+ head = stack [- k ]
34
+ stack [- k - 1 ].next = None
35
+ return head
Original file line number Diff line number Diff line change
1
+ # 36ms 97.50%
2
+
3
+ # Definition for singly-linked list.
4
+ # class ListNode:
5
+ # def __init__(self, x):
6
+ # self.val = x
7
+ # self.next = None
8
+
9
+ class Solution :
10
+ def swapPairs (self , head ):
11
+ """
12
+ :type head: ListNode
13
+ :rtype: ListNode
14
+ """
15
+ if not head :
16
+ return []
17
+
18
+ mid = head
19
+
20
+ if mid .next is not None :
21
+ next_node = mid .next
22
+ mid .next = next_node .next
23
+ next_node .next = mid
24
+ head = next_node
25
+ else :
26
+ return head
27
+
28
+ while True :
29
+ if mid .next is not None :
30
+ last_node = mid
31
+ mid = mid .next
32
+ else :
33
+ return head
34
+ if mid .next is not None :
35
+ next_node = mid .next
36
+ mid .next = next_node .next
37
+ next_node .next = mid
38
+
39
+ last_node .next = next_node
40
+ else :
41
+ return head
You can’t perform that action at this time.
0 commit comments