Leetcode Note: Linked List Easy Section Part 2

来源:互联网 发布:心理学网络课程 编辑:程序博客网 时间:2024/04/29 16:43

Merge Two 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.

解析:将两个排列好的链表合并

代码:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if(l1==null&&l2==null)return null;        ListNode dummy = new ListNode(0);        ListNode pre = dummy;        while(l1!=null && l2!=null)        {            if(l1.val<l2.val)            {dummy.next = l1;l1 = l1.next;}            else            {dummy.next = l2;l2 = l2.next;}            dummy = dummy.next;        }        if(l1==null)dummy.next=l2;        if(l2==null)dummy.next=l1;        return pre.next;    }}



Remove Duplicates from Sorted List 

描述:Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 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 deleteDuplicates(ListNode head) {        if(head==null)return null;                ListNode dummy = new ListNode(0);        dummy.next = head;        ListNode left = dummy;        ListNode right = head;                while(right.next!=null)        {            if(right.val==right.next.val)                right = right.next;            else                {left.next = right;right = right.next;left = left.next;}        }        left.next = right;        return dummy.next;    }}



Intersection of Two Linked Lists

描述:

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2                   ↘                     c1 → c2 → c3                   ↗            B:     b1 → b2 → b3

begin to intersect at node c1.

解析:先通过遍历得出两个链表的长度差gap,然后在更长的那个链表设一个指针,让它移动gap步。然后在短的链表头设一个指针,两个指针同时前进,如果两个指针相遇了,说明有交点,反之则否。

代码:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {                if(headA==null||headB==null)return null;                ListNode a = headA;        ListNode b = headB;                int lengthA = 0;        int lengthB = 0;        int gap = 0;                while(a.next!=null){a=a.next;lengthA++;}        while(b.next!=null){b=b.next;lengthB++;}                if(a!=b)return null;                a=headA;        b=headB;                if(lengthA>lengthB)        {            gap = lengthA - lengthB;            for(int i=0;i<gap;i++)a=a.next;        }        else        {            gap = lengthB - lengthA;            for(int i=0;i<gap;i++)b=b.next;        }                while(true)        {            if(a==b)return a;            else {a=a.next;b=b.next;}        }    }}



0 0
原创粉丝点击