LinkedLists Flashcards

LinkedLists

1
Q

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

A
  • Create dummy node as fake head
  • keep check of carry, modulus 10, carry = sum / 10
  • return dummy.next
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
    ListNode cur1 = l1;
    ListNode cur2 = l2;
    int carry = 0;
    while(cur1 != null || cur2 != null || carry != 0){
        int num1 = cur1 == null ? 0 : cur1.val;
        int num2 = cur2 == null ? 0 : cur2.val;
            int sum = num1 + num2 + carry;            
            int newNum = sum % 10;
            carry = sum / 10;
            cur.next = new ListNode(newNum);
            cur = cur.next;
        cur1 = cur1 == null ? null: cur1.next;
        cur2 = cur2 == null ? null: cur2.next;
    }

    return dummy.next;
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

A
  • Dummy head for new list
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null) return l2;
        else if(l2 == null) return l1;
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        while(l1 != null && l2!= null){
            if(l1.val <= l2.val){
                curr.next = l1;
                l1 = l1.next;
            }else {
                curr.next = l2;
                l2 = l2.next;
            }
            curr = curr.next;
        }
        curr.next = l1 == null? l2:l1;
        return dummy.next;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

Input:
{“$id”:”1”,”next”:{“$id”:”2”,”next”:null,”random”:{“$ref”:”2”},”val”:2},”random”:{“$ref”:”2”},”val”:1}

Explanation:
Node 1’s value is 1, both of its next and random pointer points to Node 2.
Node 2’s value is 2, its next pointer points to null and its random pointer points to itself.

Note:

You must return the copy of the given head as a reference to the cloned list.

A

public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) return null;

Map map = new HashMap();

  // loop 1. copy all the nodes
  RandomListNode node = head;
  while (node != null) {
    map.put(node, new RandomListNode(node.label));
    node = node.next;
  }
  // loop 2. assign next and random pointers
  node = head;
  while (node != null) {
    map.get(node).next = map.get(node.next);
    map.get(node).random = map.get(node.random);
    node = node.next;
  }

return map.get(head);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You may not modify the values in the list’s nodes, only nodes itself may be changed.

Example 1:

Given 1->2->3->4, reorder it to 1->4->2->3.
Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

A

//This question is a combination of Reverse a linked list I & II. //It should be pretty straight forward to do it in 3 steps :)

public void reorderList(ListNode head) {
if(head==null||head.next==null) return;

            //Find the middle of the list
            ListNode p1=head;
            ListNode p2=head;
            while(p2.next!=null&amp;&amp;p2.next.next!=null){ 
                p1=p1.next;
                p2=p2.next.next;
            }
            //Reverse the half after middle  1->2->3->4->5->6 to 1->2->3->6->5->4
            ListNode preMiddle=p1;
            ListNode preCurrent=p1.next;
            while(preCurrent.next!=null){
                ListNode current=preCurrent.next;
                preCurrent.next=current.next;
                current.next=preMiddle.next;
                preMiddle.next=current;
            }
            //Start reorder one by one  1->2->3->6->5->4 to 1->6->2->5->3->4
            p1=head;
            p2=preMiddle.next;
            while(p1!=preMiddle){
                preMiddle.next=p2.next;
                p2.next=p1.next;
                p1.next=p2;
                p1=p2.next;
                p2=preMiddle.next;
            }
        }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly