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;} } }}
- Leetcode Note: Linked List Easy Section Part 2
- Leetcode Note: Linked List Easy Section Part 1
- leetcode note--leetcode 141 Linked List Cycle
- [Leetcode 203, Easy] Remove Linked List Elements
- [Leetcode 206, Easy] Reverse Linked List
- [Leetcode 234, Easy] Palindrome Linked List
- <LeetCode><Easy> 203 Remove Linked List Elements
- <LeetCode><Easy> 328. Odd Even Linked List
- Leetcode 141. Linked List Cycle (Easy) (cpp)
- Leetcode 206. Reverse Linked List (Easy) (cpp)
- Leetcode 234. Palindrome Linked List (Easy) (cpp)
- LeetCode 206. Reverse Linked List (Easy)
- 【Leetcode-Easy-206】Reverse Linked List
- 【Leetcode-Easy-141】Linked List Cycle
- 【Leetcode-Easy-234】Palindrome Linked List
- LeetCode-Easy刷题(32) Linked List Cycle
- leetcode 206[easy]-Reverse Linked List
- Linked List:Easy
- 如何利用CSS把网页整体变成黑白灰度的
- CentOS6.3编译fibjs
- 安卓avd安装时显示error:waiting for device
- struts2+jQuery+ajax调用演示
- h264: mp4 to annexb in ffmpeg
- Leetcode Note: Linked List Easy Section Part 2
- Java常用类库
- 分布式系统设计原理与方案
- chapter11test5
- A. Exam
- 使用PythonCharm简易安装python统计包及UnicodeDecodeError,numpy.distutils.systeminfo.blas/lapacknotfounderror
- 类模板相互引用的问题(错误:缺少类型说明符-假定为int。注意:C++不支持默认int)
- 约瑟夫问题(循环链表)
- jQuery中html()、val()、text()的区别; javascript中innerHTMl、innerText、outHTML的区别