Coder Social home page Coder Social logo

-add_two_numbers's Introduction

-Add_Two_Numbers

#add_two_numbers

两个链表的数相加(由尾到头),结果也存入一个链表,同样反向表示。

开始被反向表示迷惑。其实直接可以从头到尾依次相加(在判断不为空后),设一个进位值,用 相加结果/10 表示即可,而真正存入的值为 相加结果%10 表示。

主要熟悉链表的使用方法。

#Merge Two Sorted Lists

在while中进行l1与l2的值的插入。若1的值小,插入1,否则插入2。while的条件是两个链表均为true。 最后将没放完的链表直接赋给p->next。

#Remove Duplicates from Sorted List

去掉链表中重复的元素,并返回该链表。

首先定义一个指向该链表的指针,在while循环中,若指针当前指向的值与其next指向的值相等,就将其next指向下一个元素的指针赋给当前指向的next的指针。 即p->next=p->next->next,即删去了后面重复的元素,仅保留了第一个。

#Intersection of Two Linked Lists

找两个链表指向的相同的结点。即p==q。若没有返回null。

定义两个指针分别指向两个链表。

while指针均不为空且不相等时 {  指向下一个;  若相等,随意返回一个;   //重点!在没有目标结点时,两个均为空,即相等,即返回null  如果为null,说明指向了链表尾部,此时让其指向另一个链表的头部;   //这样保证了两个指针距离目标结点的距离相等 }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.