目录
一,反转链表(206)
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
思路1:三指针法
开始让n1指向空指针,n2指向第一个的位置,用n3标记n2下一个节点的位置,让n2的下一个节点指向n1,然后让n1指向n2所指向的地址,n2指向n3 所指向的地址,最后让n3指向下一个节点的位置(当n3指向空时就不在执行这一步)。循环执行上面的步骤,直到最后一步完成后n2指向空为止。代码如下:
struct ListNode* reverseList(struct ListNode* head){
//三指针法
if(head == NULL)
{
return NULL;
}
struct ListNode* n1,*n2,*n3;
n1 = NULL;
n2 = head;
n3 = n2->next;
while(n2)
{
n2->next = n1;
n1 = n2;
n2 = n3;
if(n3)
n3=n3->next;
}
return n1;
}
思路二:取节点插到新链表
开始让newhead指向NULL,cur指向头结点,用next 标记cur的下一个节点,让cur指向newhead,然后让newhead指向cur,cur向next,next指向下一个节点。循环上面步骤,当cur指向空指针停止。代码如下:
//取节点插到新链表
struct ListNode* cur = head,*newhead = NULL;
while(cur)
{
struct ListNode *next = cur->next;
cur->next = newhead;
newhead = cur;
cur = next;
}
return newhead;
}
二,合并两个有序列表(2)
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
不带哨兵位思路:
将两个链表依次比较,选取更小的数来尾插。这里用cur1标记第一个链表,用cur2标记第二个链表,用head当作头节点,方便返回,用tail当作尾节点,方便尾插。先将cur1指向的数与cur2比较,小的尾插到tail后面,然后刚才比较小的链表cur向后指向一位,依次重复上面的步骤,直到有一个链表结束的时候停止,第一次尾插的时候,没有哨兵位需要将cur指向的值赋值给head,tail,来当做新链表的头结点和尾节点。代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
//不带哨兵位
if(list1==NULL)
{
return list2;
}
if(list2==NULL)
{
return list1;
}
struct ListNode* cur1 = list1,*cur2 = list2;
struct ListNode* head = NULL,*tail = NULL;
while(cur1 && cur2)
{
if(cur1->val < cur2->val)
{
if(head==NULL)
{
head = tail = cur1;
}
else
{
tail->next = cur1;
tail= tail->next;
}
cur1 = cur1->next;
}
else{
if(head == NULL)
{
head = tail = cur2;
}
else
{
tail->next = cur2;
tail= tail->next;
}
cur2 = cur2->next;
}
}
if(cur1)
{
tail->next = cur1;
}
if(cur2)
{
tail->next = cur2;
}
return head;
}
带哨兵位的思路:
思路与上面相同,只是这里创建了一个哨兵位来作为头结点,不存储数据,只负责“站岗”。最后用完需要将哨兵位free掉。代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
if(list1==NULL)
{
return list2;
}
if(list2==NULL)
{
return list1;
}
struct ListNode* cur1 = list1,*cur2 = list2;
struct ListNode* guard = NULL,*tail=NULL;
guard = tail= (struct ListNode*)malloc(sizeof(struct ListNode));
tail->next = NULL;
while(cur1 && cur2)
{
if(cur1->val < cur2->val)
{
tail->next = cur1;
tail= tail->next;
cur1 = cur1->next;
}
else{
tail->next = cur2;
tail= tail->next;
cur2 = cur2->next;
}
}
if(cur1)
{
tail->next = cur1;
}
if(cur2)
{
tail->next = cur2;
}
struct ListNode*head = guard->next;
free(guard);
return head;
}