Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const swapPairs = head => {
- if (!head) return null;
- let newHead = head.next || head;
- let current = head;
- let last = null;
- while (current) {
- const temp = current;
- current = swapPair(last, current);
- last = temp;
- }
- return newHead;
- };
- /**
- * @return {*} next
- */
- const swapPair = (last, node) => {
- const temp = node.next;
- const next = temp && temp.next;
- if (temp) {
- if (last) last.next = temp;
- temp.next = node;
- node.next = null;
- } else {
- if (last) last.next = node;
- }
- return next;
- };
Add Comment
Please, Sign In to add comment