LeetCode之链表
2. Add Two Numbers
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.链表长度相同时,且最后一个节点相加有进位的情况;
3.链表长度不等,短链表最后一位有进位的情况,且进位后长链表也有进位的情况;
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1==null){ return l2; } if(l2==null){ return l1; } int len1 = getLength(l1); int len2 = getLength(l2); ListNode head = null; ListNode plus = null; if(len1>=len2){ head = l1; plus = l2; }else{ head = l2; plus = l1; } ListNode p = head; int carry = 0; int sum = 0; while(plus!=null){ sum = p.val + plus.val+carry; carry = sum/10; p.val = sum%10; if(p.next==null&&carry!=0){ ListNode node = new ListNode(carry); p.next = node; carry = 0; } p = p.next; plus = plus.next; } while(p!=null&&carry!=0){ sum = p.val+carry; carry = sum/10; p.val = sum%10; if(p.next==null&&carry!=0){ ListNode node = new ListNode(carry); p.next = node; carry=0; } p = p.next; } return head; } public int getLength(ListNode l){ if(l==null){ return 0; } int len = 0; while(l!=null){ ++len; l = l.next; } return len; } }
递归解法:
复杂度
时间O(n) 空间(n) 递归栈空间
思路
从末尾到首位,对每一位对齐相加即可。技巧在于如何处理不同长度的数字,以及进位和最高位的判断。这里对于不同长度的数字,我们通过将较短的数字补0来保证每一位都能相加。递归写法的思路比较直接,即判断该轮递归中两个ListNode是否为null。
- 全部为null时,返回进位值
- 有一个为null时,返回不为null的那个ListNode和进位相加的值
- 都不为null时,返回 两个ListNode和进位相加的值
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { return helper(l1,l2,0); } public ListNode helper(ListNode l1, ListNode l2, int carry){ if(l1==null && l2==null){ return carry == 0? null : new ListNode(carry); } if(l1==null && l2!=null){ l1 = new ListNode(0); } if(l2==null && l1!=null){ l2 = new ListNode(0); } int sum = l1.val + l2.val + carry; ListNode curr = new ListNode(sum % 10); curr.next = helper(l1.next, l2.next, sum / 10); return curr; } }
迭代法:
复杂度
时间O(n) 空间(1)
思路
迭代写法相比之下更为晦涩,因为需要处理的分支较多,边界条件的组合比较复杂。过程同样是对齐相加,不足位补0。迭代终止条件是两个ListNode都为null。
注意
- 迭代方法操作链表的时候要记得手动更新链表的指针到next
- 迭代方法操作链表时可以使用一个dummy的头指针简化操作
- 不可以在其中一个链表结束后直接将另一个链表串接至结果中,因为可能产生连锁进位
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); if(l1 == null && l2 == null){ return dummyHead; } int sum = 0, carry = 0; ListNode curr = dummyHead; while(l1!=null || l2!=null){ int num1 = l1 == null? 0 : l1.val; int num2 = l2 == null? 0 : l2.val; sum = num1 + num2 + carry; curr.next = new ListNode(sum % 10); curr = curr.next; carry = sum / 10; l1 = l1 == null? null : l1.next; l2 = l2 == null? null : l2.next; } if(carry!=0){ curr.next = new ListNode(carry); } return dummyHead.next; } }
21. Merge Two Sorted 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.
思路1:1)考虑特殊情况,两个链表至少有一个为空;
2)考虑两个链表长度不一样的情况;
/** * 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) return l2; if(l2==null) return l1; ListNode head; if(l1.val<=l2.val){ head = new ListNode(l1.val); l1 = l1.next; }else{ head = new ListNode(l2.val); l2 = l2.next; } ListNode node = head; while(l1!=null&&l2!=null){ if(l1.val<=l2.val){ node.next = l1; node = node.next; l1 = l1.next; }else{ node.next = l2; node = node.next; l2 = l2.next; } } if(l1!=null){ node.next = l1; } if(l2!=null){ node.next = l2; } return head; } }
206. Reverse Linked List
Reverse a singly linked list.
思路:1)首先判断头节点是否为空,为空则直接返回;
2)
public class Solution { public ListNode reverseList(ListNode head) { if(head==null) return null; ListNode newHead = new ListNode(head.val); ListNode p = head.next; while(p!=null){ ListNode node = new ListNode(p.val); node.next = newHead; p = p.next; newHead = node; } return newHead; } }
Sort a linked list in O(n log n) time using constant space complexity.
思路:要求时间复杂度为O(nlogn),空间复杂度为O(1);
1.考虑使用归并排序;
2.归并排序需要找到中点,考虑使用快慢指针;
3.归并中排序;
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode sortList(ListNode head) { if(head==null||head.next==null) return head; ListNode mid = getMid(head); ListNode right = sortList(mid.next); mid.next = null; ListNode left = sortList(head); return mergeList(left,right); } public ListNode getMid(ListNode head){ ListNode slow = head; ListNode fast = head.next; while(fast!=null&&fast.next!=null){ slow = slow.next; fast = fast.next.next; } return slow; } public ListNode mergeList(ListNode left,ListNode right){ if(left==null) return right; if(right==null) return left; ListNode head = null; if(left.val<=right.val){ head = left; left = left.next; }else{ head = right; right = right.next; } ListNode node = head; while(left!=null&&right!=null){ if(left.val<=right.val){ node.next = left; left = left.next; }else{ node.next = right; right = right.next; } node = node.next; } if(left!=null){ node.next = left; } if(right!=null){ node.next = right; } return head; } }
Sort a linked list using insertion sort.
使用插入的方式对链表进行排序:
插入排序的思路如下:当前数与其前面的有序数依次进行比较,找到适当的位置插入,以此类推,得到最终结果;
此题的思路是:
1)如果head为空或者head.next为空,则直接返回head;
2)使用一个节点cur遍历当前待排序的节点,利用另一个节点pre从头开始遍历,若pre的值<=cur的值且两者不同,pre=pre.next;否则,记录第一个>cur的节点及其值,再遍历此节点到cur节点,调整节点值得位置,将cur的值交换到第一个>cur的节点处,直到链表遍历完;
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode insertionSortList(ListNode head) { if(head==null||head.next==null){ return head; } ListNode cur = head; while(cur!=null){ ListNode pre = head; while(pre.val<=cur.val&&pre!=cur){ pre = pre.next; } int firstVal = pre.val; ListNode mark = pre; while(pre!=cur){ int nextVal = pre.next.val; int tmp = nextVal; pre.next.val = firstVal; firstVal = tmp; pre = pre.next; } mark.val = firstVal; cur = cur.next; } return head; } }
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given{1,2,3,4}, reorder it to{1,4,2,3}.
解题思路:
先使用快慢指针找到链表的中点,反转后半部分的链表,再以中点为断点进行前后两部分交叉合并;
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public void reorderList(ListNode head) { if(head==null||head.next==null) return; ListNode fast = head.next; ListNode slow = head; while(fast!=null&&fast.next!=null){ slow = slow.next; fast = fast.next.next; } ListNode pre = reverseList(slow.next); slow.next = pre; ListNode p = head; ListNode q = slow.next; while(q!=null&&p!=null){ slow.next = q.next; q.next = p.next; p.next = q; p = q.next; q = slow.next; } }
//反转单链表 public ListNode reverseList(ListNode head){ if(head==null||head.next==null) return head; ListNode cur = head.next; ListNode pre = head; while(cur!=null){ ListNode node = cur.next; cur.next = pre; pre = cur; cur = node; } head.next = cur; return pre; } }