LeetCode 链表类题目总结

LeetCode中跟链表相关的问题总结

1.链表相加 add two numbers

1.1 问题

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 思路

1.先判断两个结点是否为空;如果某一个list为空,则返回另一个list即可;

2.定义一个相加后的值carry,其进位值为carry/10 加到链表中的值为carry%10;

3.注意在相加之后,需要判断更长的链表部分直接添加到结果中,主要进位即可;

4.两个数相加 carry最多为1;

1.3 代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //逆序----
        /*ListNode psum = new ListNode(0);
        ListNode ptail = psum;
        ListNode pcur;
        ListNode p1 = l1;
        ListNode p2 = l2;
        int carry = 0;
        int value;
        while(p1 != null && p2 != null){
            value = p1.val + p2.val + carry;
            carry = value/10;
            value = value%10;
            pcur = new ListNode(value);
            ptail.next = pcur;
            ptail = pcur;
            
            p1 = p1.next;
            p2 = p2.next;
        }
        ListNode p = p1 >= p2 ? p1:p2;
        
        while(p != null){
            value = p.val + carry;
            carry = value/10;
            value = value%10;
            pcur = new ListNode(value);
            ptail.next = pcur;
            ptail = pcur;
            p = p.next;
        }
        
        if(carry!=0){
            ptail.next = new ListNode(carry);
        }
        return psum;
        //////////////////////////////////////////
        if(l1==null)
            return l2;
        if(l2==null)
            return l1;
        ListNode head = new ListNode(0);
        ListNode p = head;
         
        int temp = 0;
        while(l1!=null||l2!=null||temp!=0){
            if(l1!=null){
                temp+=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                temp+=l2.val;
                l2=l2.next;
            }
             
            p.next=new ListNode(temp%10);
            p=p.next;
            temp/=10;          
        }
        return head.next;*/
        if(l1==null)
            return l1;
        if(l2 == null)
            return l2;
        ListNode l3 = new ListNode(0);
        ListNode p = l3;
        int carry = 0;
        while(l1!=null || l2!=null){
            if(l1!=null){
                carry+=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                carry+=l2.val;
                l2=l2.next;
            }
            p.next = new ListNode(carry%10);
            carry = carry/10;
            p = p.next;
        }
        if(carry == 1){
            p.next = new ListNode(1);
        }
        return l3.next;
        
    }
}

 

2.链表的部分翻转

1.1 问题

 

1.2 思路

 

1.3 代码

 

3.排序链表中去重(仅删除重复元素)

1.1 问题

 

1.2 思路

 

1.3 代码

 

4.排序链表去重(重复元素全部删除)

1.1 问题

 

1.2 思路

 

1.3 代码

 

5.链表划分

1.1 问题

 

1.2 思路

 

1.3 代码

 

6.单链表公共结点问题

1.1 问题

 

1.2 思路

 

1.3 代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(leetcode)