【day02】之链表操作

题目 5:反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

思路很简单,直接从最后一个结点逐渐指向第一个,并每次返回指向的第一个节点:

答案 1:

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode newNode = new ListNode();
        newNode = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newNode;
    }
}

如果使用迭代,就得额外设置两个结点:


class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}

题目 6:合并两个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

思路很简单,因为链表的定义是递归的,因此它的很多算法都可以用递归来做,因此这道题最好也是使用递归:

答案 1:

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
//        输入:l1 = [1,2,4], l2 = [1,3,4]
//        输出:[1,1,2,3,4,4]
        while(list1 != null || list2 != null){
            if(list1 == null){
                return list2;
            }else if(list2 == null){
                return list1;
            }else if(list1.val < list2.val){
                list1.next = mergeTwoLists(list1.next, list2);
                return list1;
            }else {
                list2.next = mergeTwoLists(list1, list2.next);
                return list2;
            }
        }
        return null;
    }
}

题目 7:排序链表

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

这里采用归并排序:

struct ListNode* merge(struct ListNode* head1, struct ListNode* head2) {
    if(!head1){
        return head2;
    }else if(!head2){
        return head1;
    }else if(head1->val < head2->val){
        head1->next = merge(head1->next, head2);
        return head1;
    }else{
        head2->next = merge(head1, head2->next);
        return head2;
    }
}

struct ListNode* toSortList(struct ListNode* head, struct ListNode* tail) {
    if(head == NULL || head->next == NULL){
        return head;
    }
    if(head->next == tail){
        head->next = NULL;
        return head;
    }
    struct ListNode *slow = head, *fast = head;
    // 寻找中点~
    while (fast != tail) {
        slow = slow->next;
        fast = fast->next;
        if (fast != tail) {
            fast = fast->next;
        }
    }
    struct ListNode* mid = slow;
    return merge(toSortList(head, mid), toSortList(mid, tail));
}

struct ListNode* sortList(struct ListNode* head) {
    return toSortList(head, NULL);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值