1. https://leetcode-cn.com/problems/linked-list-cycle/

时间复杂度:O(n)
思路:快慢指针,快指针每次走两步,慢指针每次走一步,如果相遇代表是环形链表,反之不是。

  1. var hasCycle = function(head) {
  2. let p1 = head
  3. let p2 = head
  4. while(p1 && p2 && p2.next){
  5. p1 = p1.next
  6. p2 = p2.next.next
  7. if(p1 === p2){
  8. return true
  9. }
  10. }
  11. return false
  12. };

2. https://leetcode-cn.com/problems/swap-nodes-in-pairs/

时间复杂度:O(n)
image.png

  1. var swapPairs = function(head) {
  2. let demmyHead = new ListNode(0)
  3. demmyHead.next = head
  4. let temp = demmyHead
  5. while(temp.next !== null && temp.next.next !==null){
  6. let node1 = temp.next
  7. let node2 = temp.next.next
  8. temp.next = node2
  9. node1.next = node2.next
  10. node2.next = node1
  11. temp = node1
  12. }
  13. return demmyHead.next
  14. };

3. https://leetcode-cn.com/problems/reverse-linked-list/

时间复杂度:O(n)
方法1:迭代
CE498AD8-B492-456B-8A70-6E4C6B373C51.png

  1. /**
  2. * Definition for singly-linked list.
  3. * function ListNode(val) {
  4. * this.val = val;
  5. * this.next = null;
  6. * }
  7. */
  8. /**
  9. * @param {ListNode} head
  10. * @return {ListNode}
  11. */
  12. var reverseList = function(head) {
  13. let cur = head;
  14. let pre = null;
  15. while(cur){
  16. const curNext = cur.next
  17. cur.next = pre
  18. pre = cur
  19. cur = curNext
  20. }
  21. return pre
  22. };

方法2:递归

  1. let reverseList = (head) =>{
  2. let reverse = (pre, cur) => {
  3. if(!cur) return pre;
  4. // 保存 next 节点
  5. let next = cur.next;
  6. cur.next = pre;
  7. reverse(cur, next);
  8. }
  9. return reverse(null, head);
  10. }

4. https://leetcode-cn.com/problems/merge-two-sorted-lists/

时间复杂度:O(n)
方法1:递归,小的接着大的就行了

  1. var mergeTwoLists = function(l1, l2) {
  2. if(l1 === null){
  3. return l2
  4. }else if (l2 === null){
  5. return l1
  6. }else if(l1.val < l2.val){
  7. l1.next = mergeTwoLists(l1.next,l2)
  8. return l1
  9. }else{
  10. l2.next = mergeTwoLists(l1,l2.next)
  11. return l2
  12. }
  13. };

迭代

  1. var mergeTwoLists = function(l1, l2) {
  2. let head = new ListNode()
  3. let cur = head
  4. while(l1 && l2){
  5. if(l1.val < l2.val){
  6. cur.next = l1
  7. l1 = l1.next
  8. }else{
  9. cur.next = l2
  10. l2 = l2.next
  11. }
  12. cur = cur.next
  13. }
  14. cur.next = l1!==null?l1:l2
  15. return head.next
  16. };

5. (203) https://leetcode-cn.com/problems/remove-linked-list-elements/submissions/

时间复杂度:O(n)
创建一个虚拟头节点放在链表最前面,遍历链表,如果当前值等于输入值,则链表跳过当前值,否则继续走。

  1. var removeElements = function(head, val) {
  2. let dummyHead = new ListNode()
  3. dummyHead.next = head
  4. let temp = dummyHead
  5. while(temp.next !== null){
  6. if(temp.next.val === val){
  7. temp.next = temp.next.next
  8. }else{
  9. temp = temp.next
  10. }
  11. }
  12. return dummyHead.next
  13. };

6. (83) https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/

  1. /**
  2. * @param {ListNode} head
  3. * @return {ListNode}
  4. */
  5. const deleteDuplicates = function(head) {
  6. // 设定 cur 指针,初始位置为链表第一个结点
  7. let cur = head;
  8. // 遍历链表
  9. while(cur != null && cur.next != null) {
  10. // 若当前结点和它后面一个结点值相等(重复)
  11. if(cur.val === cur.next.val) {
  12. // 删除靠后的那个结点(去重)
  13. cur.next = cur.next.next;
  14. } else {
  15. // 若不重复,继续遍历
  16. cur = cur.next;
  17. }
  18. }
  19. return head;
  20. };

7. (82) https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/

  1. const deleteDuplicates = function(head) {
  2. // 极端情况:0个或1个结点,则不会重复,直接返回
  3. if(!head || !head.next) {
  4. return head
  5. }
  6. // dummy 登场
  7. let dummy = new ListNode()
  8. // dummy 永远指向头结点
  9. dummy.next = head
  10. // cur 从 dummy 开始遍历
  11. let cur = dummy
  12. // 当 cur 的后面有至少两个结点时
  13. while(cur.next && cur.next.next) {
  14. // 对 cur 后面的两个结点进行比较
  15. if(cur.next.val === cur.next.next.val) {
  16. // 若值重复,则记下这个值
  17. let val = cur.next.val
  18. // 反复地排查后面的元素是否存在多次重复该值的情况
  19. while(cur.next && cur.next.val===val) {
  20. // 若有,则删除
  21. cur.next = cur.next.next
  22. }
  23. } else {
  24. // 若不重复,则正常遍历
  25. cur = cur.next
  26. }
  27. }
  28. // 返回链表的起始结点
  29. return dummy.next;
  30. };