Linked List

2. Add Two Numbers


You are given two linked lists representing two non-negative numbers. 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.

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

将两个链表中所存数据相加,并存入新的链表中。h指向新链表头,*t指向新链表尾,二维指针便于处理新链表的建立。子函数get获得链表节点中的值,分别存入x和y中,相加后存入新的节点,carry用于处理进位。

class Solution {
public:
    int get(ListNode* &l){
        int x=0;
        if(l != NULL){
            x=l->val;
            l=l->next;
        }
        return x;
    }
    
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int x=0,y=0,carry=0,sum=0;
        ListNode *h=NULL,**t=&h;
        while(l1!=NULL || l2!=NULL){
            x=get(l1);
            y=get(l2);
            sum=carry+x+y;
            ListNode *node=new ListNode(sum%10);
            *t=node;
            t=(&node->next);
            carry=sum/10;
        }
        if(carry > 0){
            ListNode *node=new ListNode(carry%10);
            *t=node;
        }
        return h;
    }
};


19. Remove Nth Node From End of List


Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.
要求只遍历一遍。用两个指针遍历,前指针先走n步,然后两个指针一起走,当前指针走到尾时,后指针走到倒数第n个节点的前一个节点位置。

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head==NULL || n<=0){
            return NULL;
        }
        ListNode *p1,*p2;
        p1=p2=head;
        for(int i=0;i<n;i++){
            p2=p2->next;
            if(p2==NULL) return head->next;
        }
        while(p2->next!=NULL){
            p2=p2->next;
            p1=p1->next;
        }
        p1->next=p1->next->next;
        return head;
    }
};


21. Merge Two Sorted Lists


Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

合并两个有序链表。可用递归或非递归实现。

递归实现:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
	if(l1==NULL) return l2;
        if(l2==NULL) return l1;
        if(l1->val<=l2->val){
            l1->next=mergeTwoLists(l1->next,l2);
            return l1;
        }else{
            l2->next=mergeTwoLists(l2->next,l1);
            return l2;
        }
    }
};
非递归实现:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *head =NULL;
        ListNode **tail=&head;
        while(l1!=NULL && l2!=NULL){
            if(l1->val<l2->val){
                *tail=l1;
                l1=l1->next;
            }else{
                *tail=l2;
                l2=l2->next;
            }
            tail=&(*tail)->next;
        }
        *tail=(l1?l1:l2);
        return head;
    }
};


24. Swap Nodes in Pairs


Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

链表节点成对交换。使用pre,cur,next三个指针作交换。
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *pre=NULL;
        for(ListNode *cur=head;cur && cur->next;cur=cur->next){
            ListNode *nex=cur->next;
            cur->next=nex->next;
            nex->next=cur;
            if(pre){
                pre->next=nex;
            }
            pre=cur;
            if(head==cur){
                head=nex;
            }
        }
        return head;
    }
};


25. Reverse Nodes in k-Group


Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

链表每k个节点逆序。按照顺序每k个逆序一次,直到剩下节点数小于k时停止。

class Solution {
public:
    ListNode* reverseList(ListNode* head,int k){
        ListNode* tailNext=head;
        while(tailNext && k>0){
            tailNext=tailNext->next;
            k--;
        }
        if(k>0) return head;
        ListNode* pre=tailNext,*cur=head;
        while(cur!=tailNext){
            ListNode *nex=cur->next;
            cur->next=pre;
            pre=cur;
            cur=nex;
        }
        return pre;
    }
    
    ListNode* reverseKGroup(ListNode* head, int k) {
        if(k<=0) return head;
        ListNode fake(0);
        fake.next=head;
        ListNode* p=&fake;
        while(p){
            p->next=reverseList(p->next,k);
            for(int i=0;p && i<k;i++){
                p=p->next;
            }
        }
        return fake.next;
    }
};




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值