File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1- # 1,499 LeetCode solutions in JavaScript
1+ # 1,500 LeetCode solutions in JavaScript
22
33[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
44
132613261718|[ Construct the Lexicographically Largest Valid Sequence] ( ./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js ) |Medium|
132713271719|[ Number Of Ways To Reconstruct A Tree] ( ./solutions/1719-number-of-ways-to-reconstruct-a-tree.js ) |Hard|
132813281720|[ Decode XORed Array] ( ./solutions/1720-decode-xored-array.js ) |Easy|
1329+ 1721|[ Swapping Nodes in a Linked List] ( ./solutions/1721-swapping-nodes-in-a-linked-list.js ) |Medium|
132913301726|[ Tuple with Same Product] ( ./solutions/1726-tuple-with-same-product.js ) |Medium|
133013311732|[ Find the Highest Altitude] ( ./solutions/1732-find-the-highest-altitude.js ) |Easy|
133113321748|[ Sum of Unique Elements] ( ./solutions/1748-sum-of-unique-elements.js ) |Easy|
Original file line number Diff line number Diff line change 1+ /**
2+ * 1721. Swapping Nodes in a Linked List
3+ * https://leetcode.com/problems/swapping-nodes-in-a-linked-list/
4+ * Difficulty: Medium
5+ *
6+ * You are given the head of a linked list, and an integer k.
7+ *
8+ * Return the head of the linked list after swapping the values of the kth node from the beginning
9+ * and the kth node from the end (the list is 1-indexed).
10+ */
11+
12+ /**
13+ * Definition for singly-linked list.
14+ * function ListNode(val, next) {
15+ * this.val = (val===undefined ? 0 : val)
16+ * this.next = (next===undefined ? null : next)
17+ * }
18+ */
19+ /**
20+ * @param {ListNode } head
21+ * @param {number } k
22+ * @return {ListNode }
23+ */
24+ var swapNodes = function ( head , k ) {
25+ let firstNode = head ;
26+ for ( let i = 1 ; i < k ; i ++ ) {
27+ firstNode = firstNode . next ;
28+ }
29+
30+ let slow = head ;
31+ let secondNode = firstNode . next ;
32+ while ( secondNode ) {
33+ slow = slow . next ;
34+ secondNode = secondNode . next ;
35+ }
36+
37+ const temp = firstNode . val ;
38+ firstNode . val = slow . val ;
39+ slow . val = temp ;
40+
41+ return head ;
42+ } ;
You can’t perform that action at this time.
0 commit comments