LeetCode之链表

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

解题思路:

1.其中一个链表为空的情况;

2.链表长度相同时,且最后一个节点相加有进位的情况;

3.链表长度不等,短链表最后一位有进位的情况,且进位后长链表也有进位的情况;

方法一:待优化

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null){
            return l2;
        }
        if(l2==null){
            return l1;
        }
        int len1 = getLength(l1);
        int len2 = getLength(l2);
        ListNode head = null;
        ListNode plus = null;
        if(len1>=len2){
            head = l1;
            plus = l2;
        }else{
            head = l2;
            plus = l1;
        }
        ListNode p = head;
        int carry = 0;
        int sum = 0;
        while(plus!=null){
            sum = p.val + plus.val+carry;
            carry = sum/10;
            p.val = sum%10;
            if(p.next==null&&carry!=0){
                ListNode node = new ListNode(carry);
                p.next = node;
                carry = 0;
            }
            p = p.next;
            plus = plus.next;
        }
        while(p!=null&&carry!=0){
            sum = p.val+carry;
            carry = sum/10;
            p.val = sum%10;
            if(p.next==null&&carry!=0){
                ListNode node = new ListNode(carry);
                p.next = node;
                carry=0;
            }
            p = p.next;
        }
        return head;
    }

    public int getLength(ListNode l){
        if(l==null){
            return 0;
        }
        int len = 0;
        while(l!=null){
            ++len;
            l = l.next;
        }
        return len;
    }
}
时间: 2024-10-01 09:13:15

LeetCode之链表的相关文章

[LeetCode系列]链表环探测问题II

给定一个链表头, 探测其是否有环, 如果没有返回NULL, 如果有返回环开始的位置. 环开始的位置定义为被两个指针指向的位置. 算法描述: 1. 快慢指针遍历, 如果到头说明无环返回NULL, 如果相遇说明有环, 进入2. 2. 慢指针回到起点, 快慢指针每次移动一格直到相遇, 返回快指针/慢指针. 代码: 1 class Solution { 2 public: 3 ListNode *detectCycle(ListNode *head) { 4 if (!head || !head->ne

【算法题 14 LeetCode 147 链表的插入排序】

算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def insertionSortList(self, head): """ :type head: ListNode

LeetCode 单链表专题 (一)

目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Partition List \([82]\) Remove Duplicates from Sorted List II \([61]\) Rotate List \([19]\) Remove Nth Node From End of List LeetCode 单链表专题 <c++> \([2]\)

LeetCode 206 链表 Reverse Linked List

LeetCode 206 链表 Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

LeetCode之“链表”:Linked List Cycle &amp;&amp; Linked List Cycle II

1. Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 刚看到这道题,很容易写出下边的程序: 1 bool hasCycle(ListNode *head) { 2 ListNode *a = head, *b = head; 3 while(a) 4 { 5 b =

LeetCode -- 判断链表中是否有环

思路: 使用两个节点,slow和fast,分别行进1步和2步.如果有相交的情况,slow和fast必然相遇:如果没有相交的情况,那么slow或fast必然有一个为null 相遇时有两种可能:1. 只是节点相交的情况,即:slow == fast但是 slow.next != fast.next2. 链表中存在环,即slow == fast 而且 slow.next == next 实现代码: public bool HasCycle(ListNode head) { // - for null

LeetCode之“链表”:Reorder List

题目链接 题目要求: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 刚看到题目,第一个冒出来的想法如下: 对应程序如下: 1 /** 2 *

LeetCode -- 推断链表中是否有环

思路: 使用两个节点.slow和fast,分别行进1步和2步.假设有相交的情况,slow和fast必定相遇:假设没有相交的情况,那么slow或fast必定有一个为null 相遇时有两种可能:1. 仅仅是节点相交的情况,即:slow == fast可是 slow.next != fast.next2. 链表中存在环,即slow == fast 并且 slow.next == next 实现代码: public bool HasCycle(ListNode head) { // - for null