Leetcode常见链表问题及代码示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Leetcode常见链表问题及代码示例

codeg   2020-11-30 我要评论

按规定区间反转链表

思路:可以考虑成一种把前后数字的结点断开重新组合的问题

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* reverseBetween(ListNode* head, int m, int n) {
    ListNode *dummy = new ListNode(-1), *pre = dummy;
    dummy->next = head;
    for (int i = 0; i < m - 1; ++i)
      pre = pre->next;
    ListNode *cur = pre->next;
    for (int i = m; i < n; ++i) {
      ListNode *t = cur->next;
      cur->next = t->next;
      t->next = pre->next;
      pre->next = t;
    }
    return dummy->next;
  }
};

分割链表

思路:先找到一个大于或者等于给定值的节点,然后再逐个把小于他们的值放在前面。例如本例先找到4,然后再找到3,然后把小于3的值都放在其前面

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* partition(ListNode* head, int x) {
    ListNode *p=new ListNode(-1);
    p->next=head;
    ListNode *pre=p,*cur=head;
    while(pre->next&&pre->next->val<x)
      pre=pre->next;
    cur=pre;
    while(cur->next){
      if(cur->next->val<x){
        ListNode *tmp=cur->next;
        cur->next=tmp->next;
        tmp->next=pre->next;
        pre->next=tmp;
        pre=pre->next;
      }else{
        cur=cur->next;
      }
    }
    return p->next;
  }
};

逆序链表存储数相加

思路:先建立一个p结点,然后将相加生成的新结点按顺序放到p结点之后,然后再用一个新指针cur指向新链表的最后一位。设置一个进位计数res,当两个结点值相加之后,可以用sum/10来表示进位,然后以sum%10来建立新的结点。最后需要注意的是最高位的进位问题,所以while结束后要,如果res为1,则再建一个值为1的结点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode *p=new ListNode(-1),*cur=p;
    int res=0; 
    while(l1||l2){
      int val1=l1?l1->val:0;
      int val2=l2?l2->val:0;
      int sum=val1+val2+res;
      res=sum/10;
      cur->next=new ListNode(sum%10);
      cur=cur->next;
      if(l1)
        l1=l1->next;
      if(l2)
        l2=l2->next;
    }
    if(res)
      cur->next=new ListNode(1);
    return p->next;
  }
};

顺序链表存储相加

思路:这道题和第2题类似,但是链表是从前往后遍历,加法却要从最低位相加,所以可以考虑改用栈来存储放进来的数据。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    stack<int> s1, s2;
    while (l1) {
      s1.push(l1->val);
      l1 = l1->next;
    }
    while (l2) {
      s2.push(l2->val);
      l2 = l2->next;
    }
    int sum = 0;
    ListNode *res = new ListNode(0);
    while (!s1.empty() || !s2.empty()) {
      if (!s1.empty()) {
        sum += s1.top();
        s1.pop();
      }
      if (!s2.empty()) {
        sum += s2.top();
        s2.pop();
      }
      res->val = sum % 10;
      ListNode *cur = new ListNode(sum / 10);
      cur->next = res;
      res = cur;
      sum /= 10;
    }
    return res->val == 0 ? res->next : res;
  }
};

移除链表元素

思路:直接递归调用到链表末尾,然后回来,需要删除的元素将链表next指针指向下一个元素即好。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* removeElements(ListNode* head, int val) {
    if(!head) return NULL;
    head->next=removeElements(head->next,val);
    return head->val==val?head->next:head;
  }
};

删除排序链表中的重复元素

思路:递归查找,如果head的值存在且相等,那么while循环跳过后面所有值相等的结点,如果后面if还有值相等则继续进行递归。如果最后到head的值不同后,返回到head即可。<这种方式比新建链表存储时间负责度高很多>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* deleteDuplicates(ListNode* head) {
    if (!head) return head;
    if (head->next && head->val == head->next->val) {
      while (head->next && head->val == head->next->val) {
        head = head->next;
      }
      return deleteDuplicates(head->next);
    }
    head->next = deleteDuplicates(head->next);
    return head;
  }
};

删除顺序链表中的重复元素

思路:head结点的值和身后结点的值进行比较,如果值相同,则返回后面一个结点。最后回溯递归调用删除重复结点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* deleteDuplicates(ListNode* head) {
    if(!head||!head->next) return head;
    head->next=deleteDuplicates(head->next);
    return (head->val==head->next->val)?head->next:head;
  }
};

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们