链表的各种操作

链表头结点: 设置头结点是为了保证处理第一个节点和后面的节点的时候设计的算法相同,实现程序的高效性。

1 初始化链表

bool InitList(LinkList *L)
{
    (*L)=(LinkList)malloc(sizeof(Node)); /* 产生头结点,并且使L指向此头结点 */
    if(!(*L)) /* 存储分配失败 */
        return false;
    (*L)->next=NULL; /* 链表为空 */

    return true;
}

2 在链表(*L)的第i个node位置,插入元素e

/* 在链表*L的第i个位置,插入元素e */
bool ListInsert(LinkList *L,int i,ElemType e)
{
    LinkList p;
    p=*L;

    int j;
    j=1; /*  */
    while(p && j<i) /* 让j==i,让p成为第i个节点 */
    {
        p=p->next;
        ++j;
    }
    if(!p || j>i)
        return false; /* 第i个元素不存在 */
    LinkList s;
    s=(LinkList)malloc(sizeof(Node)); /* 生成新节点 */
    s->data=e;
    s->next=p->next;
    p->next=s;

    return true;
}

3 建立带表头节点的 单链线性表L (尾插法)

/* 建立带表头节点的 单链线性表L (尾插法) */
void CreateListTail(LinkList *L,int &cnt)
{
    *L=(LinkList)malloc(sizeof(Node));
    LinkList p,r;
    r=*L;

    ElemType e;
    //Node *p;
    while(cnt--)
    {
        scanf("%d",&e);
        p=(Node *)malloc(sizeof(Node));
        p->data=e;
        r->next=p;
        r=p; /* 尾插,r始终是链表最后一个node */
    }

    r->next=NULL; /* 链表建立完毕,最后一个node->next=NULL ,这个是很重要的初始化吧 */
}

4 链表的new 创建法

struct ListNode
{
    int val;
    ListNode *next;
    //ListNode(int x):val(x),next(NULL){}
};
void Create(ListNode** L,int len)
{
    ListNode* p;
    //*L=(LinkList)malloc(sizeof(ListNode));
    *L=new ListNode;
}

5 链表排序()

5.1 快速排序

图解(原图链接,请点击)

在这里插入图片描述

代码

Node* partion(Node* pBegin, Node* pEnd)
{
    if(pBegin == pEnd || pBegin->next == pEnd)
        return pBegin;

    ElemType key=pBegin->data;

    Node *p=pBegin;
    Node *q=pBegin;
    while(q != pEnd)
    {
        if(q->data<key)
        {
            p=p->next;
            swap(p->data,q->data);
        }

        q=q->next;
    }

    swap(p->data,pBegin->data);
    return p;
}

void quick_sort(Node *pBegin, Node *pEnd)
{
    if(pBegin == pEnd || pBegin->next == pEnd)
    {
        return;
    }
    Node *mid=partion(pBegin, pEnd);

    quick_sort(pBegin, mid);
    quick_sort(mid->next,pEnd);
}

Node *sortList(Node *head)
{
    if(head == NULL || head->next == NULL)
        return head;

    quick_sort(head,NULL);
    return head;
}

5.2

5.3

6 求链表的交集与并集

6.1并集(求并集,并且删除重复元素)(开了一个vector,用来存放俩个链表相同元素)

/*
* 求交集,链表A和B,修改了链表A和B
*
*
*/
void inter_sec(LinkList *A,LinkList *B)
{
    LinkList a_tmp=*A;
    LinkList b_tmp=*B;

    a_tmp=a_tmp->next;
    //b_tmp=b_tmp->next;
    vector<int> arr;//存储交集
    while(a_tmp)
    {
        while(b_tmp->next)
        {
            if(b_tmp->next->val==a_tmp->val)
            {
                arr.push_back(a_tmp->val);
                LinkList p=b_tmp->next;
                b_tmp->next=p->next;
                free(p);
                break;
            }
            b_tmp=b_tmp->next;
        }
        a_tmp=a_tmp->next;
    }

    sort(arr.begin(),arr.end());
    cout<<"A cross B is ";
    for(int i=0;i<=arr.size()-1;i++)
        cout<<arr[i]<<" ";
    cout<<endl;
}

7 链表逆序输出和删除duplicate(共同点: 三个辅助node)

链表反转

public class Solution {
    public ListNode ReverseList(ListNode head) {
        
        if(head == null || head.next == null)
            return head;
        
        ListNode pre = head;
        ListNode cur = head.next;
        ListNode nex = null;
        
        while(cur != null)
        {
            nex = cur.next; // 用来保存cur的下一个node, 以至于不丢失链表的node
                        
            cur.next = pre;             
            pre = cur;
            cur = nex; //nex派上用场                                    
        }
        
        
        head.next = null;
        
        return pre;
    }
}

删除 duplicate, 并且重复节点不保留, 一起删掉

参考文献
在这里插入图片描述

无序链表的重复项删除(利用HashSet)

HashSet方法参考文献

/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
 
public class Solution {
    /**
     * @param head: The first node of linked list.
     * @return: Head node.
     */
    public ListNode removeDuplicates(ListNode head) {
        // write your code here
        Set<Integer> set = new HashSet<>();
        ListNode node = new ListNode(0);
        ListNode newHead = node;
        
        while(head != null){
            if(!set.contains(head.val)){
                set.add(head.val);
                node.next = head;
                
                node = node.next;
            }
            head = head.next;
        }
        
        node.next = null;
        
        return newHead.next;
    }
}

8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值