[Leetcode] 2, 92, 86

2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Solution:考察链表知识,直接按十进制运算和进位即可。

Code:

/**
 * 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* ans = l1;
        ListNode* end = l1;
        int addnum = 0;
        while(l1!=NULL && l2!=NULL){
            l1->val = l1->val + l2->val + addnum;;
            addnum = l1->val / 10;
            l1->val = l1->val % 10;
            end = l1;
            l1 = l1->next;
            l2 = l2->next;
        }
        
        if(l2!=NULL){
            end->next = l2;
            end = l2;
            l1 = l2;
        }
        while(l1!=NULL){
            l1->val = l1->val + addnum;
            addnum = l1->val / 10;
            l1->val = l1->val % 10;
            end = l1;
            l1 = l1->next;
        }
        
        if(addnum!=0){
            end->next = new ListNode(addnum);
        }
        return ans;
    }
};


92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 大于等于 m 大于等于 n 大于等于 length of list.

Solution: 直接做就好,此题难点在于非常繁琐,有很多边界检查。对于m-n之间的节点,将每一个节点指向它的前一个。

Code:

/**
 * 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) {
        if(m==n) return head;
        ListNode* pre = NULL;
        ListNode* cur = head;
        ListNode* next = NULL;
        ListNode* start = NULL;
        ListNode* end = NULL;
        int count = 0;
        while(cur!=NULL){
            count++;
            cout<<count<<endl;
            if(count==m){
                start = pre;
                end = cur;
            }else if(count==n){
                if(start!=NULL) start->next = cur;
                else head = cur;
                end->next = cur->next;
            }
            
            if(count>=m && count<=n){
                next = cur->next;
                cur->next = pre;
                pre = cur;
                cur = next;
            }else{
                pre = cur;
                cur = cur->next;
            }
            
        }
        return head;
    }
};

Solution-plus: 简化代码。加入需旋转的新节点时,将其插入到m点之前即可。

Code:

/**
 * 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(-1); //避免m点就是第一点的情况
        dummy.next = head;
        int count = 0;
        ListNode* prev_m = NULL;//记录m点前一点
        ListNode* prev = &dummy;
        while(head!=NULL){
            count++;
            if(count==m){
                prev_m = prev;
            }
            if(count>m && count<=n){
                //将当前节点插入到m点之前
                prev->next = head->next;
                head->next = prev_m->next;
                prev_m->next = head;
                head = prev;
            }
            prev = head;
            head = head->next;
            
        }
        return dummy.next;
    }
};



86. Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

Solution:链表操作很容易出错,因此尽量简化操作过程。将小于x的节点放到一个链表中,大于等于x的节点放到另一个链表中,最后将两个链表连接起来。

Code:

/**
 * 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 leftdummy = ListNode(0);
        ListNode rightdummy = ListNode(0);
        ListNode* leftcur = &leftdummy;
        ListNode* rightcur = &rightdummy;
        while(head!=NULL){
            if(head->val<x){
                leftcur->next = head;
                leftcur = head;
            }else{
                rightcur->next = head;
                rightcur = head;
            }
            head = head->next;
        }
        rightcur->next = NULL;
        leftcur->next = rightdummy.next;
        return leftdummy.next;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值