Leetcode Note: Linked List Easy Section Part 2

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值